(1) sort() (2) append() /insert() (3) pop() /remove() (4) count()
sort()
: sorts the list ascending by default.
🍟 리스트안의 요소를 오름차순으로 나열한다. reverse=True
를 입력하면 내림차순으로 나열할 수 있다.
기본형
sort(reverse=True/False | key=함수)
Example
lst = ['j', 'u', 'r', 'i'] lst.sort() print(lst) #['i', 'j', 'r', 'u']
- 리스트안에 str요소와 int요소가 같이 있을 땐 사용할 수 없다.
🍩 내장함수 sorted
returns a sorted list of the specified iterable object.
기본형
sorted(iterable | reverse=True)
: 내림차순 정렬
append()
: Adds an element at the end of the list
insert()
: Adds an element at the specified position
🍟리스트의 끝/ 지정된 위치에 요소를 추가할 수 있다.
기본형
append(element) insert(position, element)
Example
lst = ['j', 'u', 'r', 'i'] lst.append('!') #맨 끝에 추가 lst.insert(2, '!') #인덱스2번에 추가
pop()
: removes the element at the specified position.
remove()
: removes the first occurrence of the element with the specified value.
🍟 지정된 위치에 있는 / 지정된 값을 갖는 첫번째 요소를 제거한다.
기본형
pop(position) remove(value)
Example
lst = ['i', 'j', 'u', 'r', 'i'] lst.pop(0) #['j', 'u', 'r', 'i'] lst.remove('i') #['j', 'u', 'r', 'i'] print(lst)
count()
: returns the number of elements with the specified value.
🍟 리스트 안에서 지정된 값을 갖는 element의 갯수를 반환한다.
Example
points = [1, 4, 2, 9, 7, 8, 9, 3, 1] x = points.count(9) #2