DEV Community

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

Posted on • Edited on

Dictionary in Python (1)

Buy Me a Coffee

*Memos:

  • My post explains dictionary (2) and copy.
  • My post explains the useful functions for a dictionary (1).
  • My post explains the useful functions for a dictionary (2).
  • My post explains the shallow and deep copy of a dictionary.
  • My post explains a list and the list with indexing.
  • My post explains a tuple.
  • My post explains a set and copy.
  • My post explains an iterator (1).
  • My post explains variable assignment.

A dictionary:

  • is the ordered collection with zero or more pairs of key:value to use non-huge data not to get MemoryError.
  • doesn't allow duplicated keys (even with different types). *The values of the last duplicated keys are used.
  • is mutable so it can be changed.
  • can have any mixed types of keys and values except a list, set and dictionary for a key.
  • can be used like a list.
  • can be unpacked with **.
  • cannot be enlarged with * and a number.
  • can be created by {}, a dictionary(dict) comprehension or dict() with or without a dictionary. *dict() cannot create a dictionary with a list, tuple, set, iterator, string or range().
  • can be accessed and changed by keying and indexing.
  • cannot be accessed and changed by slicing properly.
  • can be continuously used through multiple variables.
  • can be copied to refer to a different dictionary.

A dictionary is for non-huge data otherwise it gets MemoryError.


{} can create a dictionary as shown below:

v = {} # Empty 1D dictionary v = {'name':'John', 'age':36} # 1D dictionary v = dict(name='John', age=36) # 1D dictionary v = dict([('name', 'John'), ('age', 36)]) # 1D dictionary v = {'name':'John', 'age':36, 'name':'Anna', 'age':24} # 1D dictionary v = {'person1':{'name':'John', 'age':36}, # 2D dictionary  'person2':{'name':'Anna', 'age':24}} v = dict(person1=dict(name='John', age=36), # 2D dictionary  person2=dict(name='Anna', age=24)) v = dict([('person1', dict([('name', 'John'), ('age', 36)])), # 2D dictionary  ('person2', dict([('name', 'Anna'), ('age', 24)]))]) v = {1:'a', 1.0:'b', 1.0+0.0j:'c', True:'d'} v = {'a':'b', 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}, 'd':{'c':'d'}} v = {'':'abc', ():(1, 2, 3)} v = {0:'apple', 1:'orange', 2:'kiwi'} # 1D dictionary # No error  v = {[2, 3]:[4, 5]} # TypeError: unhashable type: 'list'  v = {{'a':'b'}:{'c':'d'}} # TypeError: unhashable type: 'dict'  v = {{2, 3}:{4, 5}} # TypeError: unhashable type: 'set'  v = {'name':'John', 'age':36} * 3 # TypeError: unsupported operand type(s) for *: 'dict' and 'int' 
Enter fullscreen mode Exit fullscreen mode

A dictionary is the ordered collection with zero or more pairs of key:value as shown below:

v = {'name':'John', 'age':36} print(v) # {'name': 'John', 'age': 36} 
Enter fullscreen mode Exit fullscreen mode

A dictionary doesn't allow duplicated keys (even with different types) as shown below. *The values of the last duplicated keys are used:

v = {'name':'John', 'age':36, 'name':'Anna', 'age':24} print(v) # {'name': 'Anna', 'age': 24} 
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 can have any mixed types of values except a list, set and dictionary for a key as shown below:

v = {'a':'b', 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}, 'd':{'c':'d'}} print(v) # {'a': 'b', 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}, 'd': {'c': 'd'}}  print(v[2], v[2.3], v[2.3+4.5j], v[True], v[(2,3)]) # 3 4.5 (6.7+8.9j) False (4, 5) 
Enter fullscreen mode Exit fullscreen mode
v = {'':'abc', ():(1, 2, 3)} print(v[''], v[()]) # abc (1, 2, 3) 
Enter fullscreen mode Exit fullscreen mode

You can use a dictionary like a list by indexing but not by slicing properly as shown below:

v = {0:'apple', 1:'orange', 2:'kiwi'} print(v[0], v[1], v[2]) # Apple Orange Kiwi  v[0] = 'APPLE' v[1] = 'ORANGE' v[2] = 'KIWI' print(v[0], v[1], v[2]) # APPLE ORANGE KIWI  v[0:2] = ['banana', 'peach'] print(v[0], v[1], v[2], v[0:2]) # apple orange kiwi ['banana', 'peach'] 
Enter fullscreen mode Exit fullscreen mode

** can unpack a dictionary as shown below:

v1 = {"fname":"John", "lname":"Smith"} v2 = {"age":36, "gender":"Male"} v3 = {**v1, **v2} print(v3) # {'fname': 'John', 'lname': 'Smith', 'age': 36, 'gender': 'Male'} 
Enter fullscreen mode Exit fullscreen mode

A dictionary(dict) comprehension can create a dictionary as shown below:

v = {x:x**2 for x in range(6)} print(v) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)