Python 목록 메서드 치트 시트

Python 목록 작업에 대한 빠른 참조 가이드입니다. 요소 추가, 삽입, 제거, 정렬 및 분할을 마스터합니다.

목록 요소 수정

목록의 항목을 추가, 업데이트 또는 제거하는 방법입니다.

방법/기능구문설명
append()my_list.append(item)목록 끝에 요소를 추가합니다.
extend()my_list.extend(iterable)iterable의 모든 요소를 목록에 추가합니다.
insert()my_list.insert(index, item)지정된 위치에 요소를 삽입합니다.
remove()my_list.remove(item)지정된 항목의 첫 번째 항목을 제거합니다. 찾을 수 없으면 ValueError가 발생합니다.
pop()my_list.pop([index])지정된 인덱스의 항목을 제거하고 반환합니다(기본값은 마지막 항목).

유틸리티 목록 및 주문

목록 검색, 정렬, 복사 방법.

방법/기능구문설명
sort()my_list.sort(reverse=False, key=None)목록을 그 자리에서 정렬합니다.
reverse()my_list.reverse()내부 목록 요소의 순서를 반대로 바꿉니다.
index()my_list.index(item)지정된 항목이 처음 나타나는 인덱스를 반환합니다.
clear()my_list.clear()목록에서 모든 요소를 제거합니다.

대화형 데모 스크립트

run_all_cheat_methods.py
에디터에서 실행
# append()
my_list.append(item)

# extend()
my_list.extend(iterable)

# insert()
my_list.insert(index, item)

# remove()
my_list.remove(item)

# pop()
my_list.pop([index])

# sort()
my_list.sort(reverse=False, key=None)

# reverse()
my_list.reverse()

# index()
my_list.index(item)

# clear()
my_list.clear()

자주 묻는 질문

추가()와 확장()의 차이점은 무엇입니까?

Append()는 목록 끝에 단일 요소로 인수를 추가하는 반면, Extension()은 인수를 반복하여 각 요소를 개별적으로 추가합니다.

목록을 수정하지 않고 어떻게 되돌릴 수 있나요?

reversed() 함수를 사용하거나 목록 분할: my_list[::-1]을 사용하십시오.

관련 주제

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.