[go: up one dir, main page]

DEV Community

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

Posted on

Dictionary functions in Python (3)

Buy Me a Coffee

*Memo:

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

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=.
# 1D dictionary
print(dict.fromkeys(['A', 'B', 'C']))
print(dict.fromkeys(['A', 'B', 'C'], None))
# {'A': None, 'B': None, 'C': None}

# 1D dictionary
print(dict.fromkeys(['A', 'B', 'C'], 'X'))
# {'A': 'X', 'B': 'X', 'C': 'X'}

# 2D dictionary
print(dict.fromkeys(['A', 'B', 'C'], {'X':'Y'}))
# {'A': {'X': 'Y'}, 'B': {'X': 'Y'}, 'C': {'X': 'Y'}}

# 3D dictionary
print(dict.fromkeys(['A', 'B', 'C'], {'X':{'Y':'Z'}}))
# {'A': {'X': {'Y': 'Z'}}, 'B': {'X': {'Y': 'Z'}}, 'C': {'X': {'Y': 'Z'}}}
Enter fullscreen mode Exit fullscreen mode

sorted() can convert a dictionary to the list which has the zero or more keys and/or values of the dictionary, then sort the list as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
  • The 2nd argument is key(Optional-Default:None-Type:Callable or NoneType).
  • The 3rd argument is reverse(Optional-Default:False) to reverse the list.
  • By default, the list which has the zero or more keys of a dictionary is used.
  • sorted() creates a copy:
    • Be careful, sorted() does shallow copy instead of deep copy as my issue.

<1D dictionary>:

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

print(sorted(v))
print(sorted(v, key=None, reverse=False))
print(sorted(v.keys()))
# ['age', 'name']

print(sorted(v.values(), key=lambda key: str(key)))
# [36, 'John']

print(sorted(v.items()))
# [('age', 36), ('name', 'John')]

print(sorted(v, reverse=True))
print(sorted(v.keys(), reverse=True))
# ['name', 'age']

print(sorted(v.values(), key=lambda key: str(key), reverse=True))
# ['John', 36]

print(sorted(v.items(), reverse=True))
# [('name', 'John'), ('age', 36)]
Enter fullscreen mode Exit fullscreen mode

<2D dictionary>:

v = {'person1':{'name':'John', 'age':36},
     'person2':{'name':'Anna', 'age':24}}

print(sorted(v))
print(sorted(v.keys()))
# ['person1', 'person2']

print(sorted(v.values(), key=lambda key: str(key)))
# [{'name': 'Anna', 'age': 24},
#  {'name': 'John', 'age': 36}]

print(sorted(v.items()))
# [('person1', {'name': 'John', 'age': 36}),
#  ('person2', {'name': 'Anna', 'age': 24})]

print(sorted(v, reverse=True))
print(sorted(v.keys(), reverse=True))
# ['person2', 'person1']

print(sorted(v.values(), key=lambda key: str(key), reverse=True))
# [{'name': 'John', 'age': 36},
#  {'name': 'Anna', 'age': 24}]

print(sorted(v.items(), reverse=True))
# [('person2', {'name': 'Anna', 'age': 24}),
#  ('person1', {'name': 'John', 'age': 36})]

print(sorted(v['person2']))
print(sorted(v['person2'].keys()))
# ['age', 'name']

print(sorted(v['person2'].values(), key=lambda key: str(key)))
# [24, 'Anna']

print(sorted(v['person2'].items()))
# [('age', 24), ('name', 'Anna')]

print(sorted(v['person2'], reverse=True))
print(sorted(v['person2'].keys(), reverse=True))
# ['name', 'age']

print(sorted(v['person2'].values(), key=lambda key: str(key), reverse=True))
# ['Anna', 24]

print(sorted(v['person2'].items(), reverse=True))
# [('name', 'Anna'), ('age', 24)]
Enter fullscreen mode Exit fullscreen mode

reversed() can return the iterator which has the reversed zero or more keys and/or values of a dictionary, then the iterator is converted to a list with list() as shown below:

*Memo:

  • The 1st argument is seq(Required:Type-Sequence):
    • Don't use seq=.
  • By default, the list which has the zero or more keys of a dictionary is used.

<1D dictionary>:

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

print(reversed(v))
# <dict_reversekeyiterator object at 0x000001F3B9E51530>

print(list(reversed(v)))
print(list(reversed(v.keys())))
# ['age', 'name']

print(list(reversed(v.values())))
# [36, 'John']

print(list(reversed(v.items())))
# [('age', 36), ('name', 'John')]
Enter fullscreen mode Exit fullscreen mode

<2D dictionary>:

v = {'person1':{'name':'John', 'age':36},
     'person2':{'name':'Anna', 'age':24}}

print(list(reversed(v)))
print(list(reversed(v.keys())))
# ['person2', 'person1']

print(list(reversed(v.values())))
# [{'name': 'Anna', 'age': 24},
#  {'name': 'John', 'age': 36}]

print(list(reversed(v.items())))
# [('person2', {'name': 'Anna', 'age': 24}),
#  ('person1', {'name': 'John', 'age': 36})]

print(list(reversed(v['person2'])))
print(list(reversed(v['person2'].keys())))
# ['age', 'name']

print(list(reversed(v['person2'].values())))
# [24, 'Anna']

print(list(reversed(v['person2'].items())))
# [('age', 24), ('name', 'Anna')]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)