Dictionary in Python
Dictionary in Python
DICTIONARY
Dictionary in Python
print(my_dict['name'])
# Output: Jack
print(my_dict.get('age'))
# Output: 26
How to change or add
elements in a dictionary?
How to change or add elements in a dictionary?
Dictionary are mutable. We can add new items or
change the value of existing items using assignment
operator.
If the key is already present, value gets updated, else a
new key: value pair is added to the dictionary.
Changing or adding elements in a
dictionary?
my_dict = {'name':'Jack', 'age': 26}
# update value
my_dict['age'] = 27
print(my_dict)
#Output: {'age': 27, 'name': 'Jack'}
# add item
my_dict['address'] = 'Downtown'
print(my_dict)
print(list(sorted(marks.keys())))
# Output: ['English', 'Math', 'Science']
Python Dictionary Comprehension
print(sorted(squares))
# Output: [1, 3, 5, 7, 9]
n k s
Tha