Python Dictionary


In this chapter, we will learn to implement a Python Dictionary.

Python Dictionary

Dictionaries are like containers that can be used for holding value or set of values (elements). The concept of a dictionary in Python is the same as the concept of a hash in several other programming languages.
The entire concept of dictionary rotates around the following two essential things:
  • Values
  • Keys
The Keys are for accessing a specific value at an address and the Values are the stuff that is at that address. This might sound a bit confusing but there is no need to worry, one example will smash it all.
  1. EmpDetails = {‘Mr.X’ : ‘Dev’ , \  
  2. ‘Mr. Y’ : ‘DB Admin’, \  
  3. ‘Mr. Z’ : ‘UI Designer’, \ }  
Explanation: Consider the preceding example here:
The preceding description clearly states what the keys are, the values, and dictionary names in the example shown above.
I hope now you guys are a bit clear.

Creating a Dictionary

A simple dictionary can be created in the following two ways.
Using Multiline statements
I already discussed multiline statements in my previous article, so for a better understanding, I suggest you guys go through the previous parts.
  1. EmpDetails = {‘Mr.X’ : ‘Dev’ , \  
  2. ‘Mr. Y’ : ‘DB Admin’, \  
  3. ‘Mr. Z’ : ‘UI Designer’, \ }  
Using "{}" brackets
I already explained that these types of statements use braces in my previous article, so for a better understanding, I suggest you guys go through the previous parts.
  1. EmpDetails = {‘Mr.X’ : ‘Dev’ , ‘Mr. Y’ : ‘DB Admin’, ‘Mr. Z’ : ‘UI Designer’ };  
Accessing Values in Dictionary: For accessing elements in the dictionary, you don't need to do any herculean task. Just go through the approach that we used to do in Lists / Tuples in Python.
We need to use one and only those square brackets "[]" along with the key or index. Let's have a look.
# Accessing Dictionary
  1. dict = {'Programmer''Business Layer''DataBase Admin''Database Layer''UI Developer''Presentation Layer'};  
  2. print ("dict['Programmer']: ", dict['Programmer'])  
  3. print ("dict['UI Developer']: ", dict['UI Developer'])  

Operations | Dictionary


Dictionary | Updating

You can do updates in the dictionary in one of the following ways:
  • Adding a new element
  • Updating a new key-value
  • Modifying an existing entry
  • Deleting an existing entry
Let's explore it with an example.
# Updating Dictionary
  1. dict = {'Programmer''Business Layer''DataBase Admin''Database Layer''UI Developer''Presentation Layer'};  
# Updating a New Entry
  1. dict['Programmer'] = 'Architecture Design';   
# Showing New Entry
  1. print ("dict['Programmer']: ", dict['Programmer'])  

Dictionary | Deletes

In Python, you either delete a single element or all the dictionary elements. To do delete in a dictionary, we use a del statement.
# Deletion in Dictionary
  1. dict = {'Programmer''Business Layer''DataBase Admin''Database Layer''UI Developer''Presentation Layer'};  
  2. del dict['UI Developer'];   
  3. dict.clear(); # remove all entries in dict  
  4. del dict ; # delete entire dictionary  
  5. print ("dict['Programmer']: ", dict['programmer'])  
  6. print ("dict['Database Admin']: ", dict['Database Admin'])  
As you can see, after deletion there will be no elements in the dictionary. That's why it's giving an error.

Built-in Tuple functions

Here are some built-in tuple functions.

Dictionary | Compare

This built-in function is used for comparing two dictionaries. It compares them in terms of their elements.
  1. dict1 = ('CSK', 180)  
  2. dict2 = ('MI', 175)  
  3. print (cmp(dict1, dict2))  
  4. print (cmp(dict2, dict1))  
-1, 1

Dictionary | Length

As the name of this built-in function suggests, it is used for getting the length of any tuple value from a dictionary.
  1. tup = len ('C# Corner')  
  2. print (a)  
Output: 9


In the next chapter, we will learn to Python Exception Handling.
Abhishek Jaiswal
124 20k 9.3m
Next » Python Exception Handling