LIST and TUPLE

yozzum·2023년 10월 15일
0

● 공통점

  1. 여러 데이터를 담을 수 있는 컨테이너형 변수이다.
lst = [1,2,3]
tpl = (1,2,3)
  1. 인덱스를 통해 특정 요소에 접근할 수 있다.
lst[0] # 1
tpl[0] # 1
  1. iterable 하다.
for item in lst:
	pass
for item in tpl:
	pass

● 차이점

  1. LIST는 mutable(가변), TUPLE은 immutable(불변)
lst[0] = 9 # various_list =[9, 2, 3]
tpl[0] = 9 # TypeError : 'tuple' object does not support item assignment
  1. 따라서 LIST는 DICTIONARY의 KEY로 쓸 수 없지만, TUPLE은 가능
dic = {lst: "my list"} # TypeError: unhashable type: 'List'
dic = {tpl: "my tuple"} # OK

※ If there is a list in a tuple as an element, it gives the same error as list

  1. iteration을 도는 속도가 tuple이 더 빠르다.

  2. 복사를 하는 경우 list는 새로운 메모리에 복사되지만, tuple은 복사본이 원본을 가리키고 있다.

profile
yozzum

0개의 댓글