Dictionaries in Python-Python for Beginners

Beginners Code Computer Skills Python

Dictionaries in python

Dictionaries in Python are used to implement the concept of hash-map function in Java. By hash-map function, we mean that there is a set of domain and ranges. For every value in the domain, there will be some value in the range.

In addition, we can say Dictionaries act as one-many functions in a particular Domain D. Furthermore, the values of the Domain are also called Keys while the values in range form the key-value pair in a Dictionary.

Note that the Dictionary also belongs to the class of 5 standard datatypes in python.

If the terms sound confusing, don’t worry. In fact, these concepts are very simple and will get clear as we move on.

Defining Dictionaries

A dictionary can be created by using multiple key-value pair instances. These values are enclosed in curly braces. The syntax to define a Dictionary is:

sample_dictionary = {"Name": "Mario", "Location": "Nintendo", "Job": "Coin Stacker", "Employee ID": 1985, "Age": 44,
                     "Salary": 99999}
print(sample_dictionary)

Here the attributes like Name, Location, Job, etc. are the domain values or the Keys. The corresponding values in the Dictionary are called the range. Together they form the key-value pair in a Dictionary.

Accessing values from a Dictionary

The values from a Dictionary can be accessed either by using the Keys or the domain values as they are unique in nature.

Moreover, you can use the regular format specifier technique as used in the C language.

The syntax to access values from a dictionary are as follows:

sample_dictionary = {"Name": "Mario", "Location": "Nintendo", "Job": "Coin Stacker", "Employee ID": 1985, "Age": 44,
                     "Salary": 99999}
print("Name: %s" % sample_dictionary["Name"])

print("Location: %s" % sample_dictionary["Location"])

print("Job: %s" % sample_dictionary["Job"])

print("Employee ID: %s" % sample_dictionary["Employee ID"])

print("Age: %s" % sample_dictionary["Age"])

print("Salary: %s" % sample_dictionary["Salary")

%s refers to the string type access specifier. Thus it shows that each key-value is treated as a string in a Dictionary.

Updating values of Dictionaries

The values can be modified either by direct input or we can read the values from the user and add it to the dictionary. Following is the syntax to update values in a dictionary:

sample_dictionary = {"Name": "Mario", "Location": "Nintendo", "Job": "Coin Stacker", "Employee ID": 1985, "Age": 44,
                     "Salary": 99999}
print(sample_dictionary)
print("\n")

print("Modifying the Old Dictionary......\n")

sample_dictionary["Name"] = "Tony"
sample_dictionary["Location"] = "Malibu Islands"
sample_dictionary["Job"] = "American Patriot"
sample_dictionary["Employee ID"] = 2007
sample_dictionary["Age"] = 54
sample_dictionary["Salary"] = 3500000000

print(sample_dictionary)

Deleting values from Dictionaries

Since Dictionary is mutable, we can easily delete values according to our needs too. Following syntax tells us how to perform the deletion operation:

sample_dictionary = {"Name": "Mario", "Location": "Nintendo", "Job": "Coin Stacker", "Employee ID": 1985, "Age": 44,
                     "Salary": 99999}
print(sample_dictionary)
print("\n")

print("Performing the delete operation\n")

del sample_dictionary["Location"]
del sample_dictionary["Salary"]

print(sample_dictionary)

The dictionary permanently removes these two terms when we delete them. Thus it means, that the modification applies as a permanent change.

Looping concept in Dictionaries

We can use various looping concepts to iterate in a dictionary. We prefer mostly for loop to iterate in a dictionary. Because it helps us to understand the control flow of the dictionary in an easy way. Thus it allows us to write efficient iterative programs.

In general, we access keys and values with the help of a loop. we can access them either simultaneously or individually. The following code will make this concept more clear:

sample_dictionary = {"Name": "Mario", "Location": "Nintendo", "Job": "Coin Stacker", "Employee ID": 1985, "Age": 44,
                     "Salary": 99999}

# Here we are printing Only the Keys of the Dictionary
print("To print only the Keys")
for i in sample_dictionary:
    print(i)

# Here we are printing Only the Values of the Dictionary
print("To print only the Values")
for i in sample_dictionary:
    print(sample_dictionary[i])


# Here we are printing both the Keys and Values of the Dictionary
print("To print both Values and Keys")
for i in sample_dictionary.items():
    print(i)

To print both the values and keys, we have used inbuilt items() method. This method prints the domain and range values in a column format.

Basic operations in Dictionaries

We can perform certain basic operations in a dictionary which includes finding the length of a dictionary, determining the type of keys present in the dictionary, fetching dictionary values as a tuple, etc.

The following code gives an overview of these operations:

sample_dictionary_1 = {"Name": "Mario", "Location": "Nintendo","Employee ID": 1985, "Age": 44, "Salary": 99999}

# Calculating the length of a Dictionary
length = len(sample_dictionary_1)
print(length)

# Checking the type of Keys
type_var_1 = type("Name")
print(type_var_1)

type_var_2 = type("Salary")
print(type_var_2)


# Fetching the dictionary as a Tuple
print(sample_dictionary_1.items())

Advantages

A dictionary helps us to store several values under one key value. It acts like a reference point, where with each keyword we can access the data we are wishing to. Instead of searching all the data, Dictionary helps us to restrict our search to the type of key. Inside that type of key, we only need to see the values, to find our data.