for Python dictionary
Dictionary in Python is a collection of key: value pairs, where the value is accessed using key. As dictionary is one of the data structure in Python, so we can iterate / loop through it. Consider the following to know, how you can loop through a dictionary.
for loop in dictionary Python
The 'for' loop in Python is different form the 'for' loop in the other programming languages.
In Python, the 'for' loop does not require any update to be done. It automatically update after each iteration.
Python 'for' loop syntax
Directly iterating through a dictionary
If we iterate over a dictionary directly, like we have done with other data structures, by doing so we can fetch only keys.
The variable followed by in operator will represent the key in a dictionary.
Example
Input
Accessing value using keys in dictionary with for loop
While accessing value in a dictionary we make use of the key.
Program
Input
for i in dic:
print(dic[i])
Program
Input
Iterate through a dictionary using items() method
The items() method gives both 'key: value' pairs, so we do not have to specify the key to access a value, while iterating over a dictionary.
While iterating through a dictionary with items() method we have to specify 2 variables, the 1st one represent the key and the 2nd one represent the value in a dictionary.
Program
Input
Iterate through a dictionary with keys() method
The keys() method only provide the keys in a dictionary. Iterating through it will give one key at a time.
Program
Input
With the provided keys, as shown above, we can access it's corresponding value.
Program
Input
Iterate through dictionary with values() method
When we want to fetch the values of a dictionary, we make use of value() method.
Which will only returns the values in a dictionary, each at a time while iterating over it.
Program
Input
Changing values while iterating through a dictionary
Here is an example where each value is raised to the power of 2, while iterating through a dictionary.
Program
Input
Here is how you can store the above value into a variable.
Program
Input
Filtering a dictionary while iterating through it
In this example we will filter out the odd and even numbers from the dictionary, while iterating through it.
Program
Input
Python Dictionary comprehension
With dictionary comprehension we can do many things with just 1 line of code.
Here is, what all we can do with dictionary comprehension.
Simple formatting:-
{key: value for key, value in iterable}
Creation a dictionary with dictionary comprehension
For the creation of a dictionary with dictionary comprehension we first have to define the key: value pair in a tuple/list and then placing all the key: value pairs in a list.
Like:-
[(key, value), (key2, value2), (key3, value3), …]
And then performing dictionary comprehension, by first typing 'key: value' and then iterating through the list created as defined above.
Program
Input
You can do the same by defining two list/tuples, and packing then with the zip() method.
Program
Input
Changing keys into values and values into keys with dictionary comprehension
This example will show you, how you can flip the keys and values in a dictionary while iterating through it.
Program
Input
Calculations with dictionary comprehension
Following example will show you, how you can add '2' to each value of a dictionary using dictionary comprehension.
Program
Input
Filtering with dictionary comprehension
In this we have to specify condition after the 'for' loop.
In this example using 'if' condition we will check if a value is even, with 1st dictionary comprehension and if a value is odd, with the 2nd comprehension.
Program
Input
Conclusion
Python dictionary is the collection of key: value pairs, and can be looped through it for the extraction of data.
Here we have learned how we can loop through a dictionary using for loop, fetching keys, values and both, use of items(), keys() and values() method and finally dictionary comprehension.
Post a Comment
If you have any query, feel free to ask.