✅리스트 관련 내장함수 (Built-in Method)
함수 | 설명 | 예제 | 출력 |
len(lst) | 리스트 길이 반환 | len([1, 2, 3]) | 3 |
max(lst) | 최댓값 반환 | max([1, 2, 3]) | 3 |
min(lst) | 최솟값 반환 | min([1, 2, 3]) | 1 |
sum(lst) | 합계 반환 | sum([1, 2, 3]) | 6 |
sorted(lst) | 정렬된 리스트 반환 | sorted([3, 1, 2]) | [1, 2, 3] |
any(lst) | 하나라도 참이면 True | any([0, 0, 1]) | True |
all(lst) | 모두 참이면 True | all([1, 2, 3]) | True |
enumerate(lst) | 인덱스와 요소 쌍 반환 | list(enumerate(['a', 'b'])) | [(0, 'a'), (1, 'b')] |
zip(lst1, lst2) | 리스트 병렬 묶기 | list(zip([1, 2], ['a', 'b'])) | [(1, 'a'), (2, 'b')] |
map(func, lst) | 각 요소에 함수 적용 | list(map(lambda x: x*2, [1, 2, 3])) | [2, 4, 6] |
filter(func, lst) | 조건에 맞는 요소 반환 | list(filter(lambda x: x > 1, [1, 2, 3])) | [2, 3] |
# 1. len(lst)
print(len([1, 2, 3])) # 3
# 2. max(lst)
print(max([1, 2, 3])) # 3
# 3. min(lst)
print(min([1, 2, 3])) # 1
# 4. sum(lst)
print(sum([1, 2, 3])) # 6
# 5. sorted(lst)
print(sorted([3, 1, 2])) # [1, 2, 3]
# 6. any(lst)
print(any([0, 0, 1])) # True
# 7. all(lst)
print(all([1, 2, 3])) # True
# 8. enumerate(lst)
print(list(enumerate(['a', 'b']))) # [(0, 'a'), (1, 'b')]
# 9. zip(lst1, lst2)
print(list(zip([1, 2], ['a', 'b']))) # [(1, 'a'), (2, 'b')]
# 10. map(func, lst)
print(list(map(lambda x: x*2, [1, 2, 3]))) # [2, 4, 6]
# 11. filter(func, lst)
print(list(filter(lambda x: x > 1, [1, 2, 3]))) # [2, 3]
✅리스트 관련 메서드 (List Method)
메서드 | 설명 | 예제 | 출력 |
append(x) | 리스트 끝에 요소 추가 | [1, 2].append(3) | [1, 2, 3] |
extend(iterable) | 다른 리스트 요소 추가 | [1, 2].extend([3, 4]) | [1, 2, 3, 4] |
insert(i, x) | i 위치에 요소 삽입 | [1, 3].insert(1, 2) | [1, 2, 3] |
remove(x) | 첫 번째 x 삭제 | [1, 2, 3].remove(2) | [1, 3] |
pop(i) | i 번째 요소 제거 후 반환 | [1, 2, 3].pop(1) | 2 |
clear() | 모든 요소 삭제 | [1, 2, 3].clear() | [] |
index(x) | x의 첫 번째 위치 반환 | [1, 2, 3].index(2) | 1 |
count(x) | x의 개수 반환 | [1, 2, 2, 3].count(2) | 2 |
sort() | 오름차순 정렬 | [3, 1, 2].sort() | [1, 2, 3] |
reverse() | 요소 순서 뒤집기 | [1, 2, 3].reverse() | [3, 2, 1] |
copy() | 리스트 복사 | new_lst = [1, 2, 3].copy() | new_lst → [1, 2, 3] |
# 1. append(x)
lst = [1, 2]
lst.append(3)
print(lst) # [1, 2, 3]
# 2. extend(iterable)
lst = [1, 2]
lst.extend([3, 4])
print(lst) # [1, 2, 3, 4]
# 3. insert(i, x)
lst = [1, 3]
lst.insert(1, 2)
print(lst) # [1, 2, 3]
# 4. remove(x)
lst = [1, 2, 3]
lst.remove(2)
print(lst) # [1, 3]
# 5. pop(i)
lst = [1, 2, 3]
popped = lst.pop(1)
print(popped) # 2
print(lst) # [1, 3]
# 6. clear()
lst = [1, 2, 3]
lst.clear()
print(lst) # []
# 7. index(x)
lst = [1, 2, 3]
print(lst.index(2)) # 1
# 8. count(x)
lst = [1, 2, 2, 3]
print(lst.count(2)) # 2
# 9. sort()
lst = [3, 1, 2]
lst.sort()
print(lst) # [1, 2, 3]
# 10. reverse()
lst = [1, 2, 3]
lst.reverse()
print(lst) # [3, 2, 1]
# 11. copy()
lst = [1, 2, 3]
new_lst = lst.copy()
print(new_lst) # [1, 2, 3]
'Python Basic Syntax (파이썬 기초 문법) > Method (메서드)' 카테고리의 다른 글
Set Function & Method (세트 내장함수 & 메서드) (1) | 2025.02.14 |
---|---|
Dictionray Function & Method (딕셔너리 내장함수 & 메서드) (0) | 2025.02.14 |
String Function & Method (문자열 내장함수 & 메서드) (0) | 2025.02.14 |
Numeric Type Function & Method (숫자형 내장함수 & 메서드) (0) | 2025.02.14 |