-
[Python] - List개발/Python 2022. 7. 27. 01:28
List
원소들이 연속적으로 저장되는 형태의 자료형입니다.
mutable -> reallocate 할 필요 X1. 원소 추가
# List 끝에 element 추가 / O(1) list.append(x) # List 끝에 iterable 추가 / O(len(iterable)) list.extend(iterable) # 주어진 i 위치에 항목에 삽입 / O(N) list.insert(i, x)
2. 원소 제거
# delete x's first element / 없다면, ValueError # O(N) list.remove(x) # delete index element / return Value # pop() = O(1) pop(i) = O(N) list.pop([i]) # delete List all element # O(1) list.clear() # delete List element with index # O(N) del list[:]
3. 기타
# find element index / x not in list error list.index(x) # count element x list.count(x) # sort list.sort() # reverse list.reverse() list[::-1] # 얕은 복사 # allocate same memory address list.copy() # deep copy list[:] list(s)
4. 반복
# list repetition >>> a = [5,3] >>> b = a * 3 >>> b [5, 3, 5, 3, 5, 3] # shallow copy >>> a = [[2,5]] * 3 >>> a [[2, 5], [2, 5], [2, 5]] >>> a[0].append(7) >>> a [[2, 5, 7], [2, 5, 7], [2, 5, 7]
'개발 > Python' 카테고리의 다른 글
[Python] - Powerful Function (0) 2022.07.27 [Python] - Set (0) 2022.07.27 [Python] - Dictionary (0) 2022.07.27 [Python] - Tuple (0) 2022.07.27 삼항 연산자(Ternary operator) (0) 2022.07.18