파이썬/파이썬(python) 초급

[8-5 파이썬] 리스트 메서드, list method, append(), index(), clear(), copy( ), count(), extend(), insert(), pop(), remove(), reverse(), sort()

Olivia-BlackCherry 2022. 8. 16. 12:44

파이썬은 리스트에서 쓸 수 있는 내장 메서드들이 있다. 

이 메서드를 알고 있으면

코드를 쓸 때,

간결하고 효율적으로 쓸 수 있으므로

 

이런 것들이 있다는 것을 알고

필요할 때 

찾아 쓰면 되겠다!

 

파이썬 리스트 내장메서드

메서드 
Method
설명 Description 방법
append() Adds an element at the end of the list 리스트에 요소 한개를 추가하기 list.append(x)
index() Returns the index of the first element with the specified value 리스트에서 해당 요소의 첫 인덱스를 반환하기 list.index(x)
clear() Removes all the elements from the list 리스트 안의 모든 요소를 지우기 list.clear()
copy() Returns a copy of the list 리스트 전체를 복사하여 반환하기 list.copy()
count() Returns the number of elements with the specified value 특정 요소의 개수를 세어 반환하기 list.count(x)
extend() Add the elements of a list (or any iterable), to the end of the current list 리스트 끝에 요소 여러개(sequence가 있는)를 추가하기 list.extend(list)
insert() Adds an element at the specified position 특정 위치에 요소 삽입하기 list.insert(index, x)
pop() Removes the element at the specified position 특정 위치의 요소 지우기
(한 번, 꺼내서 보여주고 지우기)
기본 인덱스값: -1 (마지막 인덱스)
list.pop(index)
remove() Removes the first item with the specified value 특정 요소 지우기
(여러 개라면 가장 첫 순서에 있는)
list.remove(x)
reverse() Reverses the order of the list 리스트 내 요소 순서를 단순하게 뒤집기 list.reverse()
sort() Sorts the list 리스트 내 요소를 순서대로 정렬하기
(알파벳, 수 등)
list.sort()

 

 

append() Adds an element at the end of the list 리스트에 요소 한개를 추가하기 list.append(x)

 

index() Returns the index of the first element with the specified value 리스트에서 해당 요소의 첫 인덱스를 반환하기 list.index(x)

 

clear() Removes all the elements from the list 리스트 안의 모든 요소를 지우기 list.clear()

 

copy() Returns a copy of the list 리스트 전체를 복사하여 반환하기 list.copy()

 

count() Returns the number of elements with the specified value 특정 요소의 개수를 세어 반환하기 list.count(x)

 

extend() Add the elements of a list (or any iterable), to the end of the current list 리스트 끝에 요소 여러개(sequence가 있는)를 추가하기 list.extend(list)

 

insert() Adds an element at the specified position 특정 위치에 요소 삽입하기 list.insert(index, x)

 

pop() Removes the element at the specified position 특정 위치의 요소 지우기
(한 번, 꺼내서 보여주고 지우기)
기본 인덱스값: -1 (마지막 인덱스)
list.pop(index)

 

remove() Removes the first item with the specified value 특정 요소 지우기
(여러 개라면 가장 첫 순서에 있는)
list.remove(x)

 

reverse() Reverses the order of the list 리스트 내 요소 순서를 단순하게 거꾸로 뒤집기 list.reverse()

 

sort() Sorts the list 리스트 내 요소를 순서대로 정렬하기
(알파벳, 수 등)
list.sort()

 

 

더 자세한 설명을 원한다면

아래의 사이트를 참고하기를 바란다. 

해당 메서드를 클릭하면 된다. 

다만 영어이다! 

https://www.w3schools.com/python/python_ref_list.asp

 

Python List/Array Methods

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com