Write Python program to perform following operations on Dictionaries: Create Dictionary, Access Dictionary elements, Update Dictionary, Delete Dictionary, Looping through Dictionary
Python dictionary is a container of key-value pairs. It is mutable and can contain
mixed types. A dictionary is an unordered collection. Python dictionaries are called
associative arrays or hash tables in other languages. The keys in a dictionary must be
immutable objects like strings or numbers. They must also be unique within a
dictionary.
Write Python program to perform following operations on Dictionaries: Create Dictionary, Access Dictionary elements, Update Dictionary, Delete Dictionary, Looping through Dictionary |
The dictionary can be created by using multiple key-value pairs enclosed with the
small brackets () and separated by the colon (:). The collections of the key-value pairs
are enclosed within the curly braces {}.
The syntax to define the dictionary is given below.
#!/usr/bin/Python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
b) Accessing Values in Dictionary: To access dictionary elements, you can use the
familiar square brackets along with the key to obtain its value.
Example:
#!/usr/bin/Python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
Output:
dict['Name']: Zara
dict['Age']: 7
c) Updating Dictionary: You can update a dictionary by adding a new entry or a keyvalue pair, modifying an existing entry, or deleting an existing entry.
Example:
#!/usr/bin/Python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
Output:
dict['Age']: 8
dict['School']: DPS School
d) Delete Dictionary Elements: You can either remove individual dictionary
elements or clear the entire contents of a dictionary. You can also delete entire
dictionary in a single operation.
To explicitly remove an entire dictionary, just use the del statement.
Example:
#!/usr/bin/Python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
#TotalProgrammingCode
Looping through Dictionary: A dictionary can be iterated using the for loop. If you
want to get both keys and the values in the output. You just have to add the keys and
values as the argument of the print statement in comma separation. After each iteration
of the for loop, you will get both the keys its relevant values in the output.
Example
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
for key, value in dict.items():
print(key, ' - ', value)
The above example contains both the keys and the values in the output. The text
‘Related to’ in the output showing the given key is related to the given value in the
output.
Name - Zara
Age - 7
Class - First
Examples
1. Write a Python script to sort (ascending and descending) a dictionary by value.
dict = {1:10,
2:100,
3:50,
4:30}
sort_element = sorted(dict.values())
print("Sorted dictionary",sort_element)
list1 = list(dict.items())
print("Reverse dictionary :",list1.reverse())
2. Write a Python script to concatenate following dictionaries to create a new one.
b. dic1 = {1:10, 2:20}
c. dic2 = {3:30, 4:40}
d. dic3 = {5:50,6:60}
dict1 = {1:10,2:20}
dict2 = {3:30,4:40}
dict3 = {5:50,6:60}
dict1.update(dict2)
dict1.update(dict3)
print("Concate Dictionary is :",dict1)