*Memo:
- My post explains a dictionary (1).
- My post explains a dictionary (2).
- My post explains a dictionary (3).
- My post explains a dictionary (4).
- My post explains a dictionary (5).
- My post explains a dictionary (7).
- My post explains a dictionary (8).
A dictionary can be iterated with a for statement as shown below:
<1D dictionary>:
dict & dict.keys():
for x in {0:1, 2:3, 4:5}: # dict
# for x in {0:1, 2:3, 4:5}.keys(): # dict.keys()
print(x)
# 0
# 2
# 4
dict.values():
for x in {0:1, 2:3, 4:5}.values(): # dict.values()
print(x)
# 1
# 3
# 5
dict.items():
for x in {0:1, 2:3, 4:5}.items(): # dict.items()
print(x)
# (0, 1)
# (2, 3)
# (4, 5)
<2D dictionary>:
dict & dict.keys():
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}: # dict
# for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.keys(): # dict.keys()
print(x)
# 0
# 3
# 6
dict.values():
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.values(): # dict.values()
for y in x: # dict
# for y in x.keys(): # dict.keys()
print(y)
# 1
# 4
# 7
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.values(): # dict.values()
for y in x.values(): # dict.values()
print(y)
# 2
# 5
# 8
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.values(): # dict.values()
for y in x.items(): # dict.items()
print(y)
# (1, 2)
# (4, 5)
# (7, 8)
dict.items():
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.items(): # dict.items()
for y in x: # tuple
if isinstance(y, dict):
for z in y: # dict
# for z in y.keys(): # dict.keys()
print(z)
else:
print(y)
# 0
# 1
# 3
# 4
# 6
# 7
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.items(): # dict.items()
for y in x: # tuple
if isinstance(y, dict):
for z in y.values(): # dict.values()
print(z)
else:
print(y)
# 0
# 2
# 3
# 5
# 6
# 8
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}.items(): # dict.items()
for y in x: # tuple
if isinstance(y, dict):
for z in y.items(): # dict.items()
print(z)
else:
print(y)
# 0
# (1, 2)
# 3
# (4, 5)
# 6
# (7, 8)
A dictionary can be unpacked with an assignment and for statement, function, * and ** as shown below:
*Memo:
- By default, the keys of a dictionary are used with iterable unpacking.
v1, v2, v3 = {0:1, 2:3, 4:5} # dict
v1, v2, v3 = {0:1, 2:3, 4:5}.keys() # dict.keys()
print(v1, v2, v3)
# 0 2 4
v1, v2, v3 = {0:1, 2:3, 4:5}.values() # dict.values()
print(v1, v2, v3)
# 1 3 5
v1, v2, v3 = {0:1, 2:3, 4:5}.items() # dict.items()
print(v1, v2, v3)
# (0, 1) (2, 3) (4, 5)
v1, *v2, v3 = {0:1, 2:3, 4:5, 6:7, 8:9, 10:11} # dict
v1, *v2, v3 = {0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.keys() # dict.keys()
print(v1, v2, v3)
# 0 [2, 4, 6, 8] 10
v1, *v2, v3 = {0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.values() # dict.values()
print(v1, v2, v3)
# 1 [3, 5, 7, 9] 11
v1, *v2, v3 = {0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.items() # dict.items()
print(v1, v2, v3)
# (0, 1) [(2, 3), (4, 5), (6, 7), (8, 9)] (10, 11)
for v1, v2, v3 in [{0:1, 2:3, 4:5}, # list(dict)
{6:7, 8:9, 10:11}]:
# for v1, v2, v3 in [{0:1, 2:3, 4:5}.keys(), # list(dict.keys())
# {6:7, 8:9, 10:11}.keys()]:
print(v1, v2, v3)
# 0 2 4
# 6 8 10
for v1, v2, v3 in [{0:1, 2:3, 4:5}.values(), # list(dict.values())
{6:7, 8:9, 10:11}.values()]:
print(v1, v2, v3)
# 1 3 5
# 7 9 11
for v1, v2, v3 in [{0:1, 2:3, 4:5}.items(), # list(dict.items())
{6:7, 8:9, 10:11}.items()]:
print(v1, v2, v3)
# (0, 1) (2, 3) (4, 5)
# (6, 7) (8, 9) (10, 11)
# list(dict)
for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11},
{12:13, 14:15, 16:17, 18:19, 20:21, 22:23}]:
# list(dict.keys())
# for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.keys(),
# {12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.keys()]:
print(v1, v2, v3)
print(v1, *v2, v3)
# 0 [2, 4, 6, 8] 10
# 0 2 4 6 8 10
# 12 [14, 16, 18, 20] 22
# 12 14 16 18 20 22
# list(dict.values())
for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.values(),
{12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.values()]:
print(v1, v2, v3)
print(v1, *v2, v3)
# 1 [3, 5, 7, 9] 11
# 1 3 5 7 9 11
# 13 [15, 17, 19, 21] 23
# 13 15 17 19 21 23
# list(dict.items())
for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.items(),
{12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.items()]:
print(v1, v2, v3)
print(v1, *v2, v3)
# (0, 1) [(2, 3), (4, 5), (6, 7), (8, 9)] (10, 11)
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11)
# (12, 13) [(14, 15), (16, 17), (18, 19), (20, 21)] (22, 23)
# (12, 13) (14, 15) (16, 17) (18, 19) (20, 21) (22, 23)
print(*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11}) # dict
print(*{0:1, 2:3, 4:5, 6:7}.keys(), # dict.keys()
*{8:9, 10:11}.keys())
# 0 2 4 6 8 10
print(*{0:1, 2:3, 4:5, 6:7}.values(), # dict.values()
*{8:9, 10:11}.values())
# 1 3 5 7 9 11
print(*{0:1, 2:3, 4:5, 6:7}.items(), # dict.items()
*{8:9, 10:11}.items())
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11)
print([*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11}]) # dict
print([*{0:1, 2:3, 4:5, 6:7}.keys(), # dict.keys()
*{8:9, 10:11}.keys()])
# [0, 2, 4, 6, 8, 10]
print([*{0:1, 2:3, 4:5, 6:7}.values(), # dict.values()
*{8:9, 10:11}.values()])
# [1, 3, 5, 7, 9, 11]
print([*{0:1, 2:3, 4:5, 6:7}.items(), # dict.items()
*{8:9, 10:11}.items()])
# [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)]
print({**{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}})
print({**{0:1, 2:3}, 4:5, **{6:7, 8:9, **{10:11}}})
# {0: 1, 2: 3, 4: 5, 6: 7, 8: 9, 10: 11}
def func(A='b', C='d'):
print(A, C)
func()
# b d
func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
func(A='B', C='D')
# B D
def func(A='b', **kwargs):
print(A, kwargs)
print(A, {0:1, **kwargs, 2:3})
func()
# b {}
# b {0: 1, 2: 3}
func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
func(A='B', C='D')
# B {'C': 'D'}
# B {0: 1, 'C': 'D', 2: 3}
Top comments (0)