for python dictionary

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.

READ MORE...


In Python, the 'for' loop does not require any update to be done. It automatically update after each iteration.


Python 'for' loop syntax

for variable in dictionary:
    statements

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

for i in dic:
    print(i)

Output

one
two
three
four
five


Accessing value using keys in dictionary with for loop

While accessing value in a dictionary we make use of the key.

Program

Input

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

for i in dic:
    print(dic[i])

Output

1
2
3
4
5


Printing both key and value.

Program

Input

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

for i in dic:
    print('Key: ', i, ' - Value: ',dic[i])

Output

Key:  one  - Value:  1
Key:  two  - Value:  2
Key:  three  - Value:  3
Key:  four  - Value:  4
Key:  five  - Value:  5

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

for key, value in dic.items():
    print('key: ', key, ' - value: ', value)

Output

key:  one  - value:  1
key:  two  - value:  2
key:  three  - value:  3
key:  four  - value:  4
key:  five  - value:  5

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

for key in dic.keys():
    print(key)

Output

one
two
three
four
five


With the provided keys, as shown above, we can access it's corresponding value.

Program

Input

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

for key in dic.keys():
    print('Key: ', key, ' - value: ', dic[key])

Output

Key:  one  - value:  1
Key:  two  - value:  2
Key:  three  - value:  3
Key:  four  - value:  4
Key:  five  - value:  5

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}


for value in dic.values():
    print(value)

Output

1
2
3
4
5

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}


for value in dic.values():
    print(value**2)

Output

1
4
9
16
25

Here is how you can store the above value into a variable.

Program

Input

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
dic2 = {}

for key, value in dic.items():
    dic2[key] = value**2

print(dic2)

Output

{'one': 1, 'two': 4, 'three': 9, 'four': 16, 'five': 25}


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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
dic_even = {}
dic_odd = {}

for key, value in dic.items():
    if value%2==0:
        dic_even[key] = value
    else:
        dic_odd[key] = value

print('Even: ', dic_even)
print('Odd:  ', dic_odd)

Output

Even:  {'two': 2, 'four': 4}
Odd:   {'one': 1, 'three': 3, 'five': 5}

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

LIST = [('one', 1), ('two', 2), ('three', 3)]

dic = {key: value for key, value in LIST}
print(dic)

Output

{'one': 1, 'two': 2, 'three': 3}


You can do the same by defining two list/tuples, and packing then with the zip() method.

Program

Input

LIST1 = ['one', 'two', 'three']
LIST2 = [1, 2, 3]

dic = {key: value for key, value in zip(LIST1, LIST2)}
print(dic)

Output

{'one': 1, 'two': 2, 'three': 3}

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

newdic = {value: key for key, value in dic.items()}
print(newdic)

Output

{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}


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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

dic = {key: value + 2 for key, value in dic.items()}
print(dic

Output

{'one': 3, 'two': 4, 'three': 5, 'four': 6, 'five': 7}

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

dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}



dic_even = {key: value for key, value in dic.items() if value%2==0}

dic_odd = {key: value for key, value in dic.items() if value%2!=0}


print('Even dic: ', dic_even)
print('Odd dic:  ', dic_odd)

Output

Even dic:  {'two': 2, 'four': 4}
Odd dic:   {'one': 1, 'three': 3, 'five': 5}




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.





If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)