Python dictionary is used to store key-value pairs.
you can define dictionary in curly bracket or by calling dict object and can assign key values to it
>>>items = dict() # or you can define like items = {}
items = {
'name': 'milk powder',
'qty':1,
'price': 200.0,
'description': 'some descriptions',
'related_items': [
#... add all related items
]
}
items = {
'name': 'milk powder',
'qty':1,
'price': 200.0,
'price': 400.0,
'description': 'some descriptions',
'related_items': [
#... add all related items
]
}
so the output of above dictionary will be
>>> items
{'name': 'milk powder',
'qty': 1,
'price': 400.0,
'description': 'some descriptions',
'related_items': []
}
As you can see the duplicate key price value is updated with the last duplicate value.
you can access dictionary items with the square bracket and key name. For example, if you want to access the names of our items then you can do it by following
>>> items['name']
'milk powder'
Dictionary is changeable which means you can change value dictionary. For example, Suppose you want to change the name of items to some other name then you can do it by following
>>> items['name'] = 'Sugar pack of 1 KG'
>>> items
{'name': 'Sugar pack of 1 KG', 'qty': 1, 'price': 400.0, 'description': 'some descriptions', 'related_items': []}
you can use calculate the length of the dictionary by using len function
>>> print(len(items))
5
Sort dictionary by key:
>>> items = dict(sorted(items.items()))
>>> items
{'description': 'some descriptions', 'name': 'milk powder', 'price': 400.0, 'qty': 1, 'related_items': []}
here few things to note
>>> items = dict(sorted(items.items(), key=lambda x:x[1]))
if you want to sort descending order by value
>>> items = dict(sorted(items.items(), key=lambda x:x[1], reverse=True))
>>> items = dict(sorted(items.items(), key=lambda x:x[1]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'float' and 'str'
Please note: dictionary value must be of the same type if you want to sort it.
Now take an example you want to sort the following dictionary
user_items_mapping={
'abc':4,
'xyz':44,
'pqr':33
}
>>> dict(sorted(user_items_mapping.items(), key=lambda x:x[1]))
{'abc': 4, 'pqr': 33, 'xyz': 44}
If you want to sort in descending order then add reverse=True
>>> dict(sorted(user_items_mapping.items(), key=lambda x:x[1], reverse=True))
{'xyz': 44, 'pqr': 33, 'abc': 4}
Now solve one Leetcode question to understand the whole python dictionary concept practically
class Solution:
def frequencySort(self, nums: List[int]) -> List[int]:
items = {}
# Count frequency of elements
for ele in nums:
if ele in items:
items[ele] += 1
else:
items[ele] = 1
# Sort elements in decending by key
items = dict(sorted(items.items(), reverse=True)) # sort by a
# Now sort elements in ascending order by frequency which means value
items = dict(sorted(items.items(), key=lambda x: x[1]))
# Now add final dictionary to result array and return
res = []
for key in items:
res += [key]*items[key]
return res