Friday, December 27, 2024
Google search engine
HomeLanguagesIterate over a dictionary in Python

Iterate over a dictionary in Python

In this article, we will cover How to Iterate Through a Dictionary in Python

Dictionary in Python is a collection of data values, used to store data values like a map, unlike other Data Types that hold only a single value as an element, Dictionary holds the key: value pair. 

There are multiple ways to iterate over a dictionary in Python.

  • Access key using the build .keys() 
  • Access key without using a key() 
  • Iterate through all values using .values()
  • Iterate through all key, and value pairs using items()
  • Access both key and value without using items()
  • Print items in Key-Value in pair 

Note: In Python version 3.6 and earlier, dictionaries were unordered. But since Python version 3.7 and later, dictionaries are ordered.

Example 1: Access key using the build .keys() 

In this example, you will see that we are using an in-build. keys() method which helps us to print all the keys in the dictionary.

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
keys = statesAndCapitals.keys()
print(keys)


Output:

dict_keys(['Gujarat', 'Maharashtra', 'Rajasthan', 'Bihar'])

Example 2: Access key without using a key() 

Iterating over dictionaries using ‘for’ loops for iterating our keys and printing all the keys present in the Dictionary.

Python3




# Python3 code to iterate through all keys in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
print('List Of given states:\n')
 
# Iterating over keys
for state in statesAndCapitals:
    print(state)


Output:

List Of given states:

Gujarat
Maharashtra
Rajasthan
Bihar

Time complexity: O(n), where n is the number of keys in the dictionary. 
Auxiliary space: O(1).

Example 3: Iterate through all values using .values()

In this example, we are using the values() method to print all the values present in the dictionary.

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
print('List Of given capitals:\n')
 
# Iterating over values
for capital in statesAndCapitals.values():
    print(capital)


Output:

List Of given capitals:

Gandhinagar
Mumbai
Jaipur
Patna

Time complexity: O(n)
Auxiliary space: O(1)

Example 4: Iterate through all key, and value pairs using items()

In this example, we are printing all the key and value pairs present in a dictionary using a items() method.

Python3




# Python3 code to iterate through all key, value
# pairs in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
print('List Of given states and their capitals:\n')
 
# Iterating over values
for state, capital in statesAndCapitals.items():
    print(state, ":", capital)


Output

List Of given states and their capitals:

Gujarat : Gandhinagar
Maharashtra : Mumbai
Rajasthan : Jaipur
Bihar : Patna

Example 5: Access both key and value without using items()

In this example, we are using a Python Loop Through a Dictionary, and with each iteration, we are obtaining the key of the directory after that, we are printing key data and we are using a key as an index to print values from the directory

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
for i in statesAndCapitals:
    print(i, '->', statesAndCapitals[i])


Output:

Gujarat -> Gandhinagar
Maharashtra -> Mumbai
Rajasthan -> Jaipur
Bihar -> Patna

Example 6: Print items in Key-Value in pair 

In this example, we are printing the key values data in the form of pairs and all the pair are enclosed in a dictionary. 

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
keys = statesAndCapitals.items()
print(keys)


Output:

dict_items([('Gujarat', 'Gandhinagar'), ('Maharashtra', 'Mumbai'), 
('Rajasthan', 'Jaipur'), ('Bihar', 'Patna')])

Example 7: Access key using unpacking of dict

In this example, you will see that we are using * to unpack the dict. The *dict method which helps us to unpack all the keys in the dictionary.

Python3




# Python3 code to iterate through all values in a dictionary
 
statesAndCapitals = {
    'Gujarat': 'Gandhinagar',
    'Maharashtra': 'Mumbai',
    'Rajasthan': 'Jaipur',
    'Bihar': 'Patna'
}
 
keys = [*statesAndCapitals]
values = '{Gujarat}-{Maharashtra}-{Rajasthan}-{Bihar}'.format(*statesAndCapitals, **statesAndCapitals)
print(keys)
print(values)


Output:

['Gujarat', 'Maharashtra', 'Rajasthan', 'Bihar']
Gandhinagar-Mumbai-Jaipur-Patna

RELATED ARTICLES

Most Popular

Recent Comments