Dictionary Python
Dictionary Python
del d['e']
3. Get the value of the key a.
d['a']
#Output: 1
4. The value of ‘a’ looks too small. Update the value of element a
to 10.
d['a'] = 10
5. Add 3 to the value of the element s.
d['s'] += d['s'] + 3
6. Check the length of the dictionary d. If it is less than 9, then
add three more elements to it.
if len(d) < 8:
d.update({'t': 21, 'h': 9, 'p':14})
print(d)
'''
Output:
{'a': 10, 'b': 5, 'c': 3, 'd': 2, 'f': 6, 's': 12, 't': 21, 'h': 9, 'p': 14}
'''
7. Make a list of all the keys.
d.keys()##Output looks like this:dict_keys([‘a’, ‘b’, ‘c’, ‘d’, ‘f’, ‘s’, ‘t’, ‘h’,
‘p’])
8. Make a list of all the values.
d.values()
##Output looks like this:
dict_values([10, 5, 3, 2, 6, 27, 21, 9, 14])
9. Find out which alphabet has the biggest value.
max = 0
max_key = 'a'
for k, v in d.items():
if v > max:
max_key = k
max = d[max_key]
print(max_key)
The answer came out to be ‘t’.
sen_map = {}
sentences = sentences.lower()
for i in sentences.split():
if i not in sen_map:
sen_map[i] = 1
sen_map[i] += 1
sen_map'''Output
{'i': 2, 'love': 2, 'my': 3, 'country.': 2, 'country': 2, 'is': 2, 'the': 5, 'best':
3, 'in': 3, 'world.': 3, 'we': 2, 'have': 2, 'athletes': 2}'''
Dictionaries can be nested like lists. Here is an example:
Gold_medals['USA']['Long Jump']
The output is 3 as we can see in the dictionary above.
Here is an example: