Methods of List
일반적으로 object에 대해 method를 호출할 경우,
해당 object의 관련 attribute의 값이 바뀌고 None을 반환하는 경우가 많다.
(단, NumPy나 Pandas 등의 경우, 해당 object의 값을 바꾸면서 바뀐 object의 참조를 반환하는 경우가 대부분임.)
때문에 method를 이용하여 변경을 수행할 경우,
반환값으로 해당 list의 object를 다시 할당시 None이 되는 문제가 발생하지 않도록 주의해야 한다.

List관련 설명.
2023.07.12 - [Python] - [Python] list (sequence type) : summary
[Python] list (sequence type) : summary
list (Sequence Type) : Summary list는 ordered mutable collection으로, collection을 위한 python data type들 중 가장 많이 사용된다. C에서의 array와 같이 가장 기본적인 collection임. 단, heterogeneous item을 가질 수 있으며,
ds31x.tistory.com
비우기
l.clear()
listobjectl의 모든 item을 제거함. empty list가 됨.None을 반환하는 점을 주의할 것: 반환값으로l을 다시 assignment해선 안된다.
값 얻어오기
l = list(range(1,10,2))
print(l[-1])
square bracket를 이용한 subscript로 개별 item에 접근 가능함.
order(순서)를 통한 indexing으로 값을 얻어옴.
(보통 dict에서 get을 이용하는 것을 권장하는 것과 차이점임.)
빼내면서 값 얻어오기
last_item = l.pop()
listobjectl의 마지막 item을 반환하면서 해당 item을 리스트에서 제거함.- argument로 pop시킬 item의 index를 줄 수 있음 (즉, 기본값은
-1)
추가하기: append, insert, extend
l.append(new_obj)
listobjectl의 마지막 item으로new_obj를 추가함.new_obj가 collection 타입인 경우, 해당 collection을 하나의 item으로 추가한다.- 여러 item을 추가하려면
extendmethod나+operator를 이용한다. None을 반환하는 점을 주의할 것: 반환값으로l을 다시 assignment해선 안된다.
l.insert(idx, new_obj)
listobjectl의idx를 인덱스로 가지는 item으로new_obj를 추가함.idx뒤의 item들은 모두 인덱스가 하나씩 증가하게 됨 : 끼어넣기None을 반환하는 점을 주의할 것: 반환값으로l을 다시 assignment해선 안된다.
l.extend(other_list)
listobjectl의 뒤부분에other_list의 item을 순서대로 추가함.None을 반환하는 점을 주의할 것: 반환값으로l을 다시 assignment해선 안된다.
item 제거하기
l.remove(obj_to_del)
listobjectl에서 최초로 등장하는obj_to_del을 제거.obj_to_del이l에 없을 경우ValueError발생.
del l[target_idx]를 통해target_idx의 item을 제거도 가능함
index 얻기
idx = l.index(obj)
listobjectl에서obj를 가르키는 인덱스 숫자를 반환 (가장 먼저 위치한 index를 반환).obj가l의 item이 아닌 경우,Value Error발생.
idx = l.index(obj,begin_idx)
listobjectl에서obj를 가르키는 인덱스 숫자를 반환하는데,begin_idx이후부터 찾기를 시작함.- 즉,
begin_idx이후 index 중obj에 해당하는 경우의 index를 반환함 (복수개인 경우 가장 먼저 위치한 idx가 반환됨) obj가l의 item이 아닌 경우,Value Error발생.
idx = l.index(obj,begin_idx,end_idx)
listobjectl에서obj를 가르키는 인덱스 숫자를 반환하는데, [begin_idx,end_dix) 범위에서 찾음.begin_idx는 inclusive:[,end_idx는 exclusive:)임.obj가l의 item이 아닌 경우,Value Error발생.
특정 값을 가지는 itme의 갯수 세기
c = l.count(obj)
listobjectl에obj가 몇개 포함되어 있는지를 count한 수를 반환.
순서 및 정렬 관련
l.reverse()
listobjectl의 item을 순서를 뒤집음.None을 반환하는 점을 주의할 것: 반환값으로l을 다시 assignment해선 안된다.
l.sort()
listobjectl을 정렬시킴 (오름차순, 알파벳순)reverse=True를 argument로 할당할 경우, 역순으로 정렬됨.None을 반환하는 점을 주의할 것: 반환값으로l을 다시 assignment해선 안된다.
2025.07.30 - [Python] - list의 sort() 메서드 와 sorted() 내장 함수
list의 sort() 메서드 와 sorted() 내장 함수
list.sort() 메서드: list를 in-place sorting을 수행함.이 메서드는 원본 리스트를 직접 수정: in-place sorting때문에 정렬된 새 리스트를 반환하지 않음: None 반환기본 사용법: sort in ascending ordersort() 메서드
ds31x.tistory.com
관련 ipynb.
https://gist.github.com/dsaint31x/6deddaa3d94db432b88b9c4c4a0d3524
py_list_methods.ipynb
py_list_methods.ipynb. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
'Python' 카테고리의 다른 글
| [Python] if, elif, else statements (0) | 2023.07.28 |
|---|---|
| [Python] Boolean Operators, Relational Operators, Membership Operators and Identity Operator (0) | 2023.07.28 |
| [Python] Closure (0) | 2023.07.15 |
| [Python] scope와 키워드 global, nonlocal (0) | 2023.07.15 |
| [Python] Nested Function (or Inner Function) (0) | 2023.07.15 |