DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Dictionary in Python (4)

Buy Me a Coffee

*Memo:

  • My post explains a dictionary (1).
  • My post explains a dictionary (2).
  • My post explains a dictionary (3).
  • My post explains dictionary functions (1).
  • My post explains dictionary functions (2).

A dictionary can be unpacked with an assignment and for statement, function, * and ** as shown below:

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)
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
# 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)
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
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)]
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode

A dictionary can be continuously used through multiple variables as shown below:

v1 = v2 = v3 = {'name':'John', 'age':36, 'gender':'Male'}
                        # Equivalent
                        # v1 = {'name':'John', 'age':36, 'gender':'Male'}
                        # v2 = v1
                        # v3 = v2
v1['name'] = 'Anna'
v2['age'] = 24
v3['gender'] = 'Female'

print(v1) # {'name': 'Anna', 'age': 24, 'gender': 'Female'}
print(v2) # {'name': 'Anna', 'age': 24, 'gender': 'Female'}
print(v3) # {'name': 'Anna', 'age': 24, 'gender': 'Female'}
Enter fullscreen mode Exit fullscreen mode

A dictionary can be shallow-copied and deep-copied as shown below:

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different outer dictionaries and the same inner dictionary.
  • is keyword can check if v1 and v2 refer to the same outer and/or inner dictionary.
  • dict.copy(), copy.copy() and dict() shallow-copy a dictionary:
    • dict.copy() has no arguments.
import copy

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = dict(v1)

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# False True

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'Y'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • v1 and v2 refer to different outer and inner dictionaries.

*Memo:

  • copy.deepcopy() deep-copies a dictionary.
  • copy.deepcopy() should be used because it's safe, deeply copying a dictionary while dict.copy(), copy.copy() and dict() aren't safe, shallowly copying a dictionary.
import copy

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = copy.deepcopy(v1)

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# False False

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)