DEV Community

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

Posted on • Edited on

Dictionary in Python (2)

Buy Me a Coffee

*Memo:

  • My post explains a dictionary (1).
  • My post explains a dictionary (3).
  • My post explains a dictionary (4).
  • My post explains a dictionary (5).
  • My post explains a dictionary (6).
  • My post explains a dictionary (7).
  • My post explains a dictionary (8).

A dictionary is the ordered mutable(unhashable) collection of zero or more pairs of keys and values(key:value) whose type is dict as shown below:

v = {'name':'John', 'age':36, 'gender':'Male'}   

print(v)
# {'name': 'John', 'age': 36, 'gender': 'Male'}

print(type(v))
# <class 'dict'>

v['name'] = 'Emily'
v['gender'] = 'Female'

print(v)
# {'name': 'Emily', 'age': 36, 'gender': 'Female'}
Enter fullscreen mode Exit fullscreen mode
v = {} # Empty dict

print(v)
# {}
Enter fullscreen mode Exit fullscreen mode

A dictionary doesn't allow duplicated keys (even with different types) as shown below:

*Memo:

  • The last duplicated key and its value are used.
v = {'name':'John', 'age':36, 'name':'Anna', 'age':24}

print(v)
# {'name': 'Anna', 'age': 24}
Enter fullscreen mode Exit fullscreen mode
v = {0:'A', 0.0:'B', 0.0+0.0j:'C', False:'D'}

print(v)
# {0: 'D'}
Enter fullscreen mode Exit fullscreen mode
v = {1:'A', 1.0:'B', 1.0+0.0j:'C', True:'D'}

print(v)
# {1: 'D'}
Enter fullscreen mode Exit fullscreen mode

A dictionary allows duplicated values as shown below:

v = {'name1':'John', 'age1':36, 'name2':'John', 'age2':36}

print(v)
# {'name1': 'John', 'age1': 36, 'name2': 'John', 'age2': 36}
Enter fullscreen mode Exit fullscreen mode
v = {'A':0, 'B':0.0, 'C':0.0+0.0j, 'D':False}

print(v)
# {'A': 0, 'B': 0.0, 'C': 0j, 'D': False}
Enter fullscreen mode Exit fullscreen mode
v = {'A':1, 'B':1.0, 'C':1.0+0.0j, 'D':True}

print(v)
# {'A': 1, 'B': 1.0, 'C': (1+0j), 'D': True}
Enter fullscreen mode Exit fullscreen mode

A dictionary can have the hashable types of keys and any types of values as shown below:

v = {'A':'a', b'A':b'a', 'BA':bytearray(b'a'),
     2:3, 2.3:4.5, 2.3+4.5j:6.7+8.9j, True:False,
     'L':[4, 5], (2, 3):(4, 5), 'S':{4, 5},
     frozenset({2, 3}):frozenset({4, 5}), 'D':{'A':'a'},
     range(2, 3):range(4, 5),
     iter([2, 3]):iter([4, 5])}
print(v)
# {'A': 'a', b'A': b'a', 'BA': bytearray(b'a'),
#  2: 3, 2.3: 4.5, (2.3+4.5j): (6.7+8.9j), True: False,
#  'L': [4, 5], (2, 3): (4, 5), 'S': {4, 5},
#  frozenset({2, 3}): frozenset({4, 5}), 'D': {'A': 'a'},
#  range(2, 3): range(4, 5),
#  <list_iterator object at 0x0000026B40AECFA0>:
#  <list_iterator object at 0x0000026B4209F310>}

print(v['A'], v[b'A'], v['BA'], v[2], v[2.3], v[2.3+4.5j],
      v[True], v['L'], v[(2, 3)], v['S'], v[frozenset({2, 3})], v['D'],
      v[range(2, 3)], v[list(v.keys())[-1]])
# a b'a' bytearray(b'a') 3 4.5 (6.7+8.9j)
# False [4, 5] (4, 5) {4, 5} frozenset({4, 5}) {'A': 'a'} range(4, 5)
# <list_iterator object at 0x0000026B4209E6E0>
Enter fullscreen mode Exit fullscreen mode
v = {'':'A', b'': b'A', ():(2, 3),
     frozenset():frozenset({2, 3}), range(0):range(2, 3),
     iter([2, 3]):iter([2, 3])}
print(v)
# {'': 'A', b'': b'A', (): (2, 3),
#  frozenset(): frozenset({2, 3}), range(0, 0): range(2, 3),
#  <list_iterator object at 0x0000026B3FD51330>:
#  <list_iterator object at 0x0000026B403883A0>}

print(v[''], v[b''], v[()],
      v[frozenset()], v[range(0)],
      v[list(v.keys())[-1]])
# A b'A' (2, 3)
# frozenset({2, 3}) range(2, 3)
# <list_iterator object at 0x0000026B3F1F3280>
Enter fullscreen mode Exit fullscreen mode

A dictionary cannot have the unhashable types of keys as shown below:

v = {[2, 3]:'val'}              # dict(list:str)
# TypeError: unhashable type: 'list'

v = {{2, 3}:'val'}              # dict(set:str)
# TypeError: unhashable type: 'set'

v = {{'A':'a'}:'val'}           # dict(dict:str)
# TypeError: unhashable type: 'dict'

v = {bytearray(b'Hello'):'val'} # dict(bytearray:str)
# TypeError: unhashable type: 'bytearray'
Enter fullscreen mode Exit fullscreen mode

A dictionary can be used with len() to get the length as shown below:

v = {'fname':'John', 'lname':'Smith', 'age':36, 'gender':'Male'}

print(len(v))
print(len(v.keys()))
print(len(v.values()))
print(len(v.items()))
# 4
Enter fullscreen mode Exit fullscreen mode

Top comments (0)