[python] 딕셔너리 언패킹(unpacking)

·2023년 4월 17일
0

[ Python ]

목록 보기
14/19

🏷️ 딕셔너리 값에 집합형 자료형이 포함된 경우

temp1 = {'a':10,'b':30,'d':50,'c':[10,20],'f':{'apple':100,'mango':500}}

print(temp1['f']['apple'])
print(temp1['c'][0])
print(temp1['c'])

>>> 100
>>> 10
>>> [10, 20]

🏷️ 언패킹(unpacking)

딕셔너리의 언패킹은 기본적으로 키값만 반환하는 특징이 있다.

1️⃣
temp1 = {'a':10,'b':30,'d':50,'c':[10,20],'f':{'apple':100,'mango':500}}

one, *other = temp1
print(f'{one}, {other}')

>>> a, ['b', 'd', 'c', 'f']
2️⃣
temp2 = {'a':10,'b':30,'d':50,'c':[10,20],'f':{'apple':100,'mango':500}}

one, *other = temp2.values()
print(one, other)

>>> 10 [30, 50, [10, 20], {'apple': 100, 'mango': 500}]

변수명은 결과 값에 영향을 미치지 않는다.

3️⃣
temp3 = {'a':10,'b':30,'d':50,'c':[10,20],'f':{'apple':100,'mango':500}}

two, *other = temp3
print(f'{two}, {other}')

three, *other = temp3.values()
print(three, other)

>>> a, ['b', 'd', 'c', 'f']
>>> 10 [30, 50, [10, 20], {'apple': 100, 'mango': 500}]
profile
https://dribbble.com/ohseyun

0개의 댓글