python을 사용하면서 가장 많이 사용하는 자료구조가 list가 아닐까 싶다.
python list를 사용하면서 기본적이지만 유용한 그리고 헷갈리는 메소드들을 정리해보았다.
a= [i for i in range(5)]
# a =[0,1,2,3,4]
a.append(5)
# a = [0,1,2,3,4,5]
a= [i for i in range(5)]
# a =[0,1,2,3,4]
add_list = [5]
a.extend(add_list)
# a = [0,1,2,3,4,5]
a= [i for i in range(5)]
# a =[0,1,2,3,4]
a.insert(1,5)
# a = [0,5,1,2,3,4]
a = [0,1,2,0,1,2]
a.remove(2)
# a= [0,1,0,1,2]
a = [0,1,2,0,1,2]
del a[4]
# a= [0,1,2,0,2]
a = [0,1,2,0,1,2]
out = a.pop(3)
# a = [0,1,2,1,2]
# out = 0
a = [1,9,3,8,4,6]
a.sort()
# a = [1, 3, 4, 6, 8, 9]
a = [1,9,3,8,4,6]
sorted_a = sorted(a)
# a = [1, 9, 3, 8, 4, 6]
# sorted_a = [1, 3, 4, 6, 8, 9]
a = [1,9,3,8,4,6]
a.reverse()
# a = [6, 4, 8, 3, 9, 1]
a = [1,9,3,8,4,6]
reversed_a = reversed(a)
# a = [1, 9, 3, 8, 4, 6]
# reversed_a = <list_reverseiterator object at 0x0000016F027138B0>
# list(reversed_a) = [6, 4, 8, 3, 9, 1]
a= [i for i in range(5)]
# a =[0,1,2,3,4]
max_val = max(a)
# max_val = 4
a= [i for i in range(5)]
# a =[0,1,2,3,4]
min_val = min(a)
# min_val = 0
a = [2,3,4,2,3,4]
val = a.index(3)
# val = 1