DEV Community

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

Posted on • Edited on

Dictionary functions in Python (2)

Buy Me a Coffee

*Memo:

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

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.

<1D dictionary>:

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

<2D dictionary>:

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.

<1D dictionary>:

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

<2D dictionary>:

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.

<1D dictionary>:

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

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

<2D dictionary>:

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

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) to set it with key:
    • Don't use default=.

<1D dictionary>:

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

<2D dictionary>:

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)