Introduction
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:
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.
Example - EmpDetails = {‘Mr.X’ : ‘Dev’ , \
- ‘Mr. Y’ : ‘DB Admin’, \
- ‘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.
Example - EmpDetails = {‘Mr.X’ : ‘Dev’ , \
- ‘Mr. Y’ : ‘DB Admin’, \
- ‘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.
Example
- 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.
Example
# Accessing Dictionary
- dict = {'Programmer': 'Business Layer', 'DataBase Admin': 'Database Layer', 'UI Developer': 'Presentation Layer'};
- print ("dict['Programmer']: ", dict['Programmer'])
- print ("dict['UI Developer']: ", dict['UI Developer'])
Output
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.
Example
# Updating Dictionary - dict = {'Programmer': 'Business Layer', 'DataBase Admin': 'Database Layer', 'UI Developer': 'Presentation Layer'};
# Updating a New Entry - dict['Programmer'] = 'Architecture Design';
# Showing New Entry - print ("dict['Programmer']: ", dict['Programmer'])
Output
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.
Example
# Deletion in Dictionary
- dict = {'Programmer': 'Business Layer', 'DataBase Admin': 'Database Layer', 'UI Developer': 'Presentation Layer'};
- del dict['UI Developer'];
- dict.clear(); # remove all entries in dict
- del dict ; # delete entire dictionary
- print ("dict['Programmer']: ", dict['programmer'])
- print ("dict['Database Admin']: ", dict['Database Admin'])
Output
Explanation
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.
Example - dict1 = ('CSK', 180)
- dict2 = ('MI', 175)
- print (cmp(dict1, dict2))
- print (cmp(dict2, dict1))
Output
-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.
Example - tup = len ('C# Corner')
- print (a)
Output: 9
Conclusion
In the next chapter, we will learn to Python Exception Handling.