In Python, a dictionary is a collection type that is used to store data in the form of key-value pairs. A dictionary is ordered, changeable, and does not allow duplicate values. The factor that makes a dictionary different from lists, tuples or sets is that in a dictionary each value has an associated key.
Note: In Python version 3.6 and earlier, the dictionaries were unordered, but as of Python 3.7, they are ordered.
A dictionary is written using curly brackets.
For example:
car = {"Brand" : "Maruti Suzuki", "Model" : "Alto"}
print(car)
Output:
{“brand” : “Maruti Suzuki”, “model” : “Alto”}
Keep in mind that “Brand” and “Model” are the keys and “Maruti Suzuki” and “Alto” are their respective values.
If we have to print a specific value, we need to use its key.
For example:
car = {"Brand" : "Maruti Suzuki", "Model" : "Alto"}
print(car["Brand"])
Output:
Maruti Suzuki
Duplicate keys
In a dictionary, values with the same keys are not allowed. If you try to do that, it will overwrite the existing value.
For example:
mydict = {"name" : "Bob", "profession" : "Gamer", "profession" : "Software Engineer"}
print(mydict)
Output:
{‘name’: ‘Bob’, ‘profession’: ‘Software Engineer’}
But, keep in mind that the keys in a dictionary are case-sensitive. So, for example, ‘name’ and ‘Name’ would be treated as different keys.
Have a look at this example
car = {
"name" : "Jaguar",
"Name" : 'Ferrari'
}
print(car['Name'])
print(car['name'])
Output:
Ferrari
Jaguar
Data type
The items in a dictionary can be of any data type. In the example given below, we have String, integer, Boolean, and list in the same dictionary.
person = {“name”: “John”, “age” : 24, isHonest : true, hobbies : [“Football”, “music”]}
Length of a dictionary
To find out the length of a dictionary, we use the len() function.
mydict = {"name" : "Bob", "profession" : "Gamer", "profession" : "Software Engineer"}
dictionary_size = len(mydict)
The dict function
A dictionary can also be created using the dict constructor.
car = dict(“Brand”: “Maruti Suzuki”, “Model”: “Alto”)
Dictionaries are defined as objects with the data type ‘dict’.
For Example:
car = dict("Brand" : "Maruti Suzuki", "Model" : "Alto")
print(type(car))
Output:
<class ‘dict’>
Nested Dictionaries
A dictionary that contains other dictionary or dictionaries is called a nested dictionary.
For example : The below example has a dictionary containing three dictionaries.
students = {
"Student1" : {
"Name" : "Alok Verma",
"age" : 15
},
"Student2" : {
"Name" : "Nishtha Shakya",
"age" : 14
},
"Student3" : {
"Name" : "Sumit Ojha",
"age" : 17
},
"Student4" : {
"Name" : "Lucky Verma",
"age" : 22
}
}
Or, have a look at this one given below. This dictionary has a dictionary containing three string key value pairs, one integer value with string type key, and a dictionary.
accountHolder = {
"Name" : "Raman Shakya",
"Age" : 34,
"City" : "Bhopal",
"Occupation" : "Software Engineer",
"account_information" : {
"account_number" : 123451020,
"IFSC" : "UBI001"
}
}
Accessing elements of a dictionary
An element of a dictionary can be assessed using its key. If we were to access elements form the above example, then, it could be something like this.
accountHolder = {
"Name" : "Raman Shakya", "Age" : 34,
"City" : "Bhopal",
"Occupation" : "Software Engineer",
"account_information" : {
"account_number" : 123451020,
"IFSC" : "UBI001"
}
}
print(accountHolder['Name'])
print(accountHolder['account_information']['account_number'])
Output:
Raman Shakya
123451020
Deleting elements from a dictionary
An element from a dictionary can be deleted using the ‘del ‘ keyword.
cityinfo = {
"Name" : "Bhopal",
"Population" : 1900000
}
print("Dictionary before deleting any element:")
print(cityinfo)
del(cityinfo['Population'])
print("After deleting an element, the new dictionary is:")
print(cityinfo)
Output:
Dictionary before deleting any element:
{‘Name’: ‘Bhopal’, ‘Population’: 1900000}
After deleting an element, the new dictionary is:
{‘Name’: ‘Bhopal’}