https://docs.python.org/3/tutorial/datastructures.html#data-structures

List data type 은 몇 가지 methods가 있다.

list.append(x)        # a[len(a):] = [x]
list.extend(iterable) # a[len(a):] = iterable
list.insert(i,x)
list.remove(x)        # value 중에 처음으로 같은 값을 찾은 그 위치의 값
list.pop(i)           # 특정 index의 값을 지우고, 남은 list를 리턴
list.clear()          # del a[:]
list.index(x[,start],end]])
list.count(x)
list.sort()
list.reverse()
list.copy()           # a[:]

List comprehensions

squares = list(map(lambda x: x**2, range(10)))
squares = [x**2 for x in range(10)]
# 원래라면 첫번째 처럼 사용해야 하지만 2번째 처럼 표현할 수 있다.

활용

스택(Stack)

  • list는 스택 처럼 사용할 수 있다.
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
# [3,4,5,6,7]
stack.pop()
# 7
 
stack
# [3,4,5,6]

큐(Queue)

  • list는 큐 처럼 사용할 수 있다. (deque)
from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")           # Terry arrives
queue.append("Graham")          # Graham arrives
queue.popleft()                 # The first to arrive now leaves
# "Eric"
queue.popleft()                 # The second to arrive now leaves
# "John"
queue   
deque(['Michael','Terry','Graham']) #Remaining queue in order of arrival

← python 3.14으로