DEV Community

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

Posted on • Edited on

Dictionary in Python (4)

Buy Me a Coffee

*Memo:

  • My post explains a dictionary (1).
  • My post explains a dictionary (2) and the dictionary with keying and copy.
  • My post explains dictionary functions (1).

get() can get a value from the dictionary by a key as shown below:

*Memo:

  • The 1st argument is key(Required-Type:Any):
    • Don't use key=.
  • The 2nd argument is default(Optional-Default:None-Type:Any) to return it if key doesn't exist:
    • Don't use default=.
v = {'name':'John', 'age':36} print(v.get('name')) # John  print(v.get('gender')) # None  print(v.get('gender', "Doesn't exist!")) # Doesn't exist! 
Enter fullscreen mode Exit fullscreen mode
v = {'person1':{'name':'John', 'age':36}, 'person2':{'name':'Anna', 'age':24}} print(v.get('person2')) # {'name': 'Anna', 'age': 24}  print(v['person2'].get('name')) # Anna 
Enter fullscreen mode Exit fullscreen mode

update() can update the dictionary as shown below:

*Memo:

  • The 1st argument is mapping or iterable(Optional-Type:Mapping or Iterable):
    • Don't use mapping= or iterable=.
  • The 2nd arguments are **other(Optional-Default:{}-Type:Any):
    • Don't use any keywords like **other=, other=, etc.
v = {'name':'John', 'age':36} v.update({'name':'Emily', 'gender':'Female'}) v.update([('name', 'Emily'), ('gender', 'Female')]) v.update(name='Emily', gender='Female') v.update({'name':'Emily'}, gender='Female') v.update([('name', 'Emily')], gender='Female') v |= {'name':'Emily', 'gender':'Female'} v |= [('name', 'Emily'), ('gender', 'Female')] print(v) # {'name': 'Emily', 'age': 36, 'gender': 'Female'} 
Enter fullscreen mode Exit fullscreen mode
v = {'person1':{'name':'John', 'age':36}, 'person2':{'name':'Anna', 'age':24}} v.update({'person1':{'name':'Emily', 'gender':'Female'}}) v.update([('person1', {'name':'Emily', 'gender':'Female'})]) v.update(person1={'name':'Emily', 'gender':'Female'}) v['person2'].update({'name':'Peter', 'gender':'Male'}) v['person2'].update([('name', 'Peter'), ('gender', 'Male')]) v['person2'].update(name='Peter', gender='Male') print(v) # {'person1': {'name': 'Emily', 'gender': 'Female'}, # 'person2': {'name': 'Peter', 'age': 24, 'gender': 'Male'}} 
Enter fullscreen mode Exit fullscreen mode

pop() can remove a pair of a key and value by key from the dictionary, throwing the value as shown below:

*Memo:

  • The 1st argument is key(Required-Type:Hashable).
  • The 2nd argument is default(Optional-Type:Any) to return its value if key doesn't exist:
    • It has no default value.
    • Don't use default=.
  • Error occurs if key doesn't exist and default isn't set.
v = {'fname':'John', 'lname':'Smith', 'age':36} print(v.pop('fname')) # John print(v) # {'lname': 'Smith', 'age': 36}  print(v.pop('age')) # 36 print(v) # {'lname': 'Smith'}  print(v.pop('gender', "Doesn't exist!")) # Doesn't exist! print(v) # {'lname': 'Smith'}  print(v.pop('gender')) # KeyError: 'gender' 
Enter fullscreen mode Exit fullscreen mode
v = {'person1':{'name':'John', 'age':36}, 'person2':{'name':'Anna', 'age':24}} print(v.pop('person1')) # {'name': 'John', 'age': 36} print(v) # {'person2': {'name': 'Anna', 'age': 24}}  print(v['person2'].pop('name')) # Anna print(v) # {'person2': {'age': 24}} 
Enter fullscreen mode Exit fullscreen mode

popitem() can remove the last pair of a key and value from the dictionary, throwing a tuple of the key and value as shown below:

*Memo:

  • It has no arguments.
  • Error occurs if the dictionary is empty.
v = {'name':'John', 'age':36, 'gender':'Male'} print(v.popitem()) # ('gender', 'Male') print(v) # {'name': 'John', 'age': 36}  print(v.popitem()) # ('age', 36) print(v) # {'name': 'John'}  print(v.popitem()) # ('name', 'John') print(v) # {}  print(v.popitem()) # KeyError: 'popitem(): dictionary is empty' 
Enter fullscreen mode Exit fullscreen mode
v = {'person1':{'name':'John', 'age':36}, 'person2':{'name':'Anna', 'age':24}} print(v.popitem()) # ('person2', {'name': 'Anna', 'age': 24}) print(v) # {'person1': {'name': 'John', 'age': 36}}  print(v['person1'].popitem()) # ('age', 36) print(v) # {'person1': {'name': 'John'}}  print(v['person1'].popitem()) # ('name', 'John') print(v) # {'person1': {}}  print(v['person1'].popitem()) # ('name', 'John') # KeyError: 'popitem(): dictionary is empty' 
Enter fullscreen mode Exit fullscreen mode

clear() can remove all pairs of a key and value from the dictionary as shown below:

*Memo:

  • It has no arguments.
v = {'name':'John', 'age':36} v.clear() print(v) # {} 
Enter fullscreen mode Exit fullscreen mode
v = {'person1':{'name':'John', 'age':36}, 'person2':{'name':'Anna', 'age':24}} v['person1'].clear() print(v) # {'person1': {}, 'person2': {'name': 'Anna', 'age': 24}}  v.clear() print(v) # {} 
Enter fullscreen mode Exit fullscreen mode

fromkeys() can create a dictionary with pairs of a key and default value as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
  • The 2nd argument is value(Optional-Default:None-Type:Any or NoneType) to set its default value with the keys of iterable:
    • Don't use value=.
print(dict.fromkeys(['a', 'b', 'c'])) print(dict.fromkeys(['a', 'b', 'c'], None)) # {'a': None, 'b': None, 'c': None}  print(dict.fromkeys(['a', 'b', 'c'], 0)) # {'a': 0, 'b': 0, 'c': 0} 
Enter fullscreen mode Exit fullscreen mode

setdefault() can set a pair of a key and default value to the dictionary if the key doesn't exist in the dictionary as shown below:

*Memo:

  • The 1st argument is key(Required-Type:Hashable):
    • Don't use key=.
  • The 2nd argument is default(Optional-Default:None-Type:Any or NoneType) to set it with key:
    • Don't use default=.
v = {'name':'John'} v.setdefault('age') v.setdefault('age', None) print(v) # {'name': 'John', 'age': None}  v.setdefault('gender', 'Male') print(v) # {'name': 'John', 'age': None, 'gender': 'Male'}  v.setdefault('name', 'Emily') v.setdefault('age', '36') v.setdefault('gender', 'Female') print(v) # {'name': 'John', 'age': None, 'gender': 'Male'} 
Enter fullscreen mode Exit fullscreen mode
v = {'person1':{'name':'John'}} v['person1'].setdefault('age') print(v) # {'person1': {'name': 'John', 'age': None}}  v.setdefault('person2') print(v) # {'person1': {'name': 'John', 'age': None}, 'person2': None}  v.setdefault('person3', {'name':'Peter'}) print(v) # {'person1': {'name': 'John', 'age': None}, # 'person2': None, # 'person3': {'name': 'Peter'}}  v['person3'].setdefault('age', '18') print(v) # {'person1': {'name': 'John', 'age': None}, # 'person2': None, # 'person3': {'name': 'Peter', 'age': '18'}}  v['person1'].setdefault('age', 36) v.setdefault('person2', {'name':'Anna'}) v['person3'].setdefault('name', 'David') print(v) # {'person1': {'name': 'John', 'age': None}, # 'person2': None, # 'person3': {'name': 'Peter', 'age': '18'}} 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)