Quick Reach
The Dictionary in Python
Python provides a data type dictionary (or Python dict) which is just like the associative arrays in some other languages. The Dictionary data types work with key – value pairs.
- A key can be of any type of objects like a number, string or a list in Python dictionary.
- The values can be accessed by using key rather than the index like in simple arrays.
This really helps in establishing a strong link between the dictionary keys and values, as explained in employee/salary example below.
If you require storing the salaries of employees in an array, an int-indexed based array can’t be the best choice and may always be confusing.
Whereas, using the key – value pair in the dictionary makes this task quite simple, as shown in the working examples of this chapter.
Syntax of creating a dictionary
Following is the syntax of Python dict:
dictionary_name = {key:value, key:value, …..}
For example
tel_dic = {‘Mike’: 1233456,’John’: 567890}
As you can see in the above example:
- Each key is separated from its value by using a ‘:’.
- keys are separated by a comma.
- Python dictionaries are enclosed in the curly brackets, i.e. {}.
Example of creating a dictionary
In the following example, we will create a python dict of employees with employee name and salary key/values.
1
2
3
4
5
6
7
|
employee = {}
employee = {‘Mike’: 10000}
employee = {‘John’: 15000}
employee = {‘Shella’: 5000}
|
You may also initialize above dictionary like that:
1
|
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
|
i.e. comma separated.
Accessing or printing elements of dictionary
Python Dictionary elements can be accessed by using the keys as shown in the example below:
1
2
3
4
5
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
print (employee[‘Mike’])
|
Output will be:
10000
How to iterate through a dictionary
We can loop through the dict just like a list. See example below of using a for loop to display employee dictionary. Note that, the Python dictionaries do not keep the order of values as created.
1
2
3
4
5
6
7
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
for emp, salary in employee.items():
print (“Salary of employee %s is %d” % (emp, salary))
|
Output will be:
Salary of employee Shella is 5000
Salary of employee Mike is 10000
Salary of employee John is 15000
Python Dictionary methods
Following are a few methods of dictionary data type with examples.
Length of dictionary
The len() method returns the total number of items in the dictionary. See example below:
1
2
3
4
5
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
print (“Length of dictionary:”, len(employee))
|
Output will be:
Length of dictionary: 3
Python dict clear() method
The dict clear() method removes all elements of a dictionary. See example below:
1
2
3
4
5
6
7
8
9
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
print (“Dictionary before clear”, employee)
employee.clear()
print (“Dictionary after clear”, employee)
|
Output will be:
Dictionary before clear {‘Shella’: 5000, ‘John’: 15000, ‘Mike’: 10000}
Dictionary after clear {}
Dictionary items method
The dict items() method returns the list of dictionary’s tuple pairs. This can be used in iterating through loops as well.
Example of using items() method
1
2
3
4
5
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
print (“Item method outputs: “, employee.items())
|
Output will be:
Item method outputs: dict_items([(‘John’, 15000), (‘Mike’, 10000), (‘Shella’, 5000)])
Dictionary keys() method
The keys() method of dictionary returns the keys in a dictionary. See example below:
1
2
3
4
5
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
print (“Keys in employee dictionary: “, employee.keys())
|
Output will be:
Keys in employee dictionary: dict_keys([‘Shella’, ‘John’, ‘Mike’])
Dictionary values() method
The dict values() method returns the values in a dictionary associated to keys.
1
2
3
4
5
|
#Dictionary example
employee ={‘Mike’: 10000, ‘John’: 15000, ‘Shella’: 5000}
print (“Values in employee dictionary: “, employee.values())
|
Output will be:
Values in employee dictionary: dict_values([15000, 10000, 5000])
Also see – Tuple in Python | The Set
Leave A Comment?