Dictionary in Python – Complete guide

We have gone through a couple of different iterable data types, in this article we will explore Dictionaries. Dictionaries are similar to sets in particular in many ways Python dictionaries are unordered collections of arbitrary key-accessed objects.  They are sometimes also called associative arrays or hash tables.

Working With Python Dictionaries

We can create a new empty dictionary using  using curly braces or by using the dict () function:

#Creation of an empty dictionary in Python
dict_sample = {}

To designate data as part of a dictionary we specify the data within the brackets according to the syntax format, where the keyname is followed by a colon, which is then followed by nesting the dictionary values within curly brackets and separating them through commas, a single dictionary can hold data of various data types and while the keynames must be unique the values belonging to different keynames within a dictionary can be repeated.

#Dictionary with consistent data types
dictionary_sample = {1: 'Apple', 2: 'Grapes'}
#Dictionary with various different data types
dictionary_sample_2 = {1: 'Grapes', 2: [2, 4, 6]}

#To display the contents of both dictionaries
print(dictionary_sample)
print(dictionary_sample_2)
Output:
{1: 'Apple', 2: 'Grapes'}
{1: 'Grapes', 2: [2, 4, 6]}

In the above example, ‘1’ and ‘2’ are the keynames which are followed by colon and then the list items. We can access the specific list items by including only the keyname within the print statement.

#Creation of Dictionary with Random Data
dictionary_sample = {
  "Name": "Adam", 
  "Country": "Indian", 
  "Year Born": 1980
} 
#Printing out Dictionary
x = dictionary_sample["Country"] 
print(x)
Output:
Indian

In the above example we print out the values of the keyname ‘Country’, hence we receive the output ‘Indian’ which belonged to the keyname in return. Suppose we want to insert an additional keyname, for example ‘Gender’ into the dictionary along with values.

#Creation of Dictionary with Random Data
dictionary_sample = {
  "Name": "Adam", 
  "Country": "Indian", 
  "Year Born": 1980
} 

#Addition of keyitem 'Gender' along with keyvalue 'Male' into 'dictionary_sample'
dictionary_sample["Gender"] = "Male" 
print(dictionary_sample)
Output:
{'Name': 'Adam', 'Country': 'Indian', 'Year Born': 1980, 'Gender': 'Male'}

Removing Items from Python Dictionary

There are several ways to remove an item from the dictionary. In this section, they will be discussed one at a time: The del keyword can be used to remove an item with a specific key. For example:

dictionary_sample = {
  "Name": "Adam", 
  "Country": "Indian", 
  "Year Born": 1980
} 
#Deletion of keyitem 'Year Born' from the dictionary
del dictionary_sample["Year Born"] 
print(dictionary_sample)
Output:
{'Name': 'Adam', 'Country': 'Indian'}

The keyword del is called, followed by the name of the dictionary. In square brackets, the dictionary is followed by the key of the item to be deleted. In this example, it is “Year Born”. The entry “Year Born” is removed from the dictionary. Another way to remove a key-value pair is pop() with a function with the record key as an argument. For example:

dictionary_sample = {
  "Name": "Adam", 
  "Country": "Indian", 
  "Year Born": 1980
} 

dictionary_sample.pop("Year Born")
print(dictionary_sample)
Output:
{'Name': 'Adam', 'Country': 'Indian'}

The function pop() was called by adding it to the name of the dictionary. In this case, the entry with the key will be deleted "Year Born". The method popitem() removes the last keyitem in the dictionary. It is automatic and you do not need to specify a specific key for it. Example:

dictionary_sample = {
"Name": "Adam",
"Country": "Indian",
"Year Born": 1980,
"Hobbies": "Reading",
}
dictionary_sample.popitem()
print(dictionary_sample)
Output:
{'Name': 'Adam', 'Country': 'Indian', 'Year Born': 1980}

Suppose we specify ‘del’ followed by the dictionary name alone, then it deletes the entire dictionary along with all it’s items. Suppose we only want to remove the items within the dictionary to leave it empty, we can use the ‘clear’ method.

dictionary_sample = {
"Name": "Adam",
"Country": "Indian",
"Year Born": 1980,
"Hobbies": "Reading",
}
dictionary_sample.clear()
print(dictionary_sample)
Output:
{}

In the next article we will look at further operations that can be performed on Dictionaries.

Leave a Comment