*Memos:
- My post explains dictionary (1).
- My post explains dictionary (2).
- My post explains the useful functions for a dictionary (2).
You can use items(), keys() and values() as shown below:
-
items()
can get zero or more keys and values from a dictionary. *There are no arguments. -
keys()
can get zero or more keys from a dictionary. *There are no arguments. -
values()
can get zero or more values from a dictionary. *There are no arguments.
v = {'name':'John', 'age':36}
print(v.keys())
# dict_keys(['name', 'age'])
print(v.values())
# dict_values(['John', 36])
print(v.items())
# dict_items([('name', 'John'), ('age', 36)])
v = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
print(v.keys())
# dict_keys(['person1', 'person2'])
print(v.values())
# dict_values([{'name': 'John', 'age': 36},
# {'name': 'Anna', 'age': 24}])
print(v.items())
# dict_items([('person1', {'name': 'John', 'age': 36}),
# ('person2', {'name': 'Anna', 'age': 24})])
print(v['person2'].keys())
# dict_keys(['name', 'age'])
print(v['person2'].values())
# dict_values(['Anna', 24])
print(v['person2'].items())
# dict_items([('name', 'Anna'), ('age', 24)])
By default, the one or more keys of a dictionary are used with an iterable unpacking as shown below:
v1 = {'name':'John', 'age':36}
v2, v3 = v1
v2, v3 = v1.keys()
print(v2) # name
print(v3) # age
v1 = {'name':'John', 'age':36}
v2, v3 = v1..values()
print(v2) # John
print(v3) # 36
v1 = {'name':'John', 'age':36}
v2, v3 = v1.items()
print(v2) # ('name', 'John')
print(v3) # ('age', 36)
v1 = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v2, v3 = v1
v2, v3 = v1.keys()
print(v2) # person1
print(v3) # person2
v1 = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v2, v3 = v1.values()
print(v2) # {'name': 'John', 'age': 36}
print(v3) # {'name': 'Anna', 'age': 24}
v1 = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v2, v3 = v1.items()
print(v2) # ('person1', {'name': 'John', 'age': 36})
print(v3) # ('person2', {'name': 'Anna', 'age': 24})
v1 = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v2, v3 = v1['person2']
v2, v3 = v1['person2'].keys()
print(v2) # name
print(v3) # age
v2, v3 = v1['person2'].values()
print(v2) # Anna
print(v3) # 24
v2, v3 = v1['person2'].items()
print(v2) # ('name', 'Anna')
print(v3) # ('age', 24)
v1 = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}.values()
v2, v3 = v1
print(v2) # {'name': 'John', 'age': 36}
print(v3) # {'name': 'Anna', 'age': 24}
v1 = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}.items()
v2, v3 = v1
print(v2) # ('person1', {'name': 'John', 'age': 36})
print(v3) # ('person2', {'name': 'Anna', 'age': 24})
You can use sorted() to sort the zero or more keys and/or values of a dictionary as a list as shown below:
*Memos:
- The 1st argument is
iterable
(Required) for an iterable. *Don't useiterable=
. - The 2nd argument is
key
(Optional-Default:None
) for a function. - The 3rd argument is
reverse
(Optional-Default:False
) to reverse a dictionary as a list. - By default, the zero or more keys of a dictionary are used with
sorted()
. -
sorted()
creates a copy. *Be careful,sorted()
does shallow copy instead of deep copy as my issue.
v = {'name':'John', 'age':36}
print(sorted(v))
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)]
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)]
You can use reversed() to reverse the zero or more keys and/or values of a dictionary as shown below:
*Memos:
- The 1st argument is
seq
(Required) for an iterable. - *Don't use
seq=
: - By default, the zero or more keys of a dictionary are used with
reversed()
.
v = {'name':'John', 'age':36}
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')]
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')]
Top comments (0)