Python Iterate Dictionary Key, Value
Last Updated :
19 Jan, 2024
In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dictionary must be unique however, we can assign the same values to different keys.
In this article, we are going to cover some basic to advanced ways of iterating over a dictionary's keys and values in Python. We will be covering all the methods to iterate Dictionary keys and values with clear and concise examples.
Syntax
d = {'key1':'value1','key2':'value2',..............}
Note : { } is used in the case of Python's Set as well as Python's Dictionary.
Iterate Dictionary's Key, Value
Let's discuss some of the common as well as efficient methods to iterate dictionary key, and value.
- Using Iteration Method
- Using .items() Function
- Using .keys() and .values()
Python Dictionary Key-Value Using Iteration
In this method, we will simply iterate over dictionary keys and retrieve their corresponding values. Below, Python code defines a function `dict_iter` that iterates over the keys of a dictionary and then demonstrates its usage on a specific dictionary.
Python3
#function to iterate over each keys and its
#corresponding values
def dict_iter(d):
for i in d:
print('KEYS: {} and VALUES: {}'.format(i,d[i]))
#Main Function
if __name__ == "__main__":
d = {"Vishu":1,"Aayush":2,"Neeraj":3,"Sumit":4}
#calling function created above
dict_iter(d)
OutputKEYS: Vishu and VALUES: 1
KEYS: Aayush and VALUES: 2
KEYS: Neeraj and VALUES: 3
KEYS: Sumit and VALUES: 4
Python Dictionary Key-Value Using .items()
In this example we will be using Python's dictionary function .items() . This function will return a list consists of dictionary's key and values. In below code , function `dict_iter` iterates over the keys and values of a dictionary, printing each key-value pair along with the dictionary items.
Python3
#function to iterate over each keys and its
#corresponding values
def dict_iter(d):
for i,j in d.items():
print('KEYS: {} and VALUES: {}'.format(i,j))
#.items()
print("\n")
print(d.items())
#Main Function
if __name__ == "__main__":
d = {"Vishu": 1,"Aayush":2,"Neeraj":3,"Sumit":4}
#calling function created above
dict_iter(d)
OutputKEYS: Vishu and VALUES: 1
KEYS: Aayush and VALUES: 2
KEYS: Neeraj and VALUES: 3
KEYS: Sumit and VALUES: 4
dict_items([('Vishu', 1), ('Aayush', 2), ('Neeraj', 3), ('Sumit', 4)])
Iterate Dictionary Key, Value Using .keys() and .values()
In this example, we are going to use Python dictionary's two most useful methods i.e. .keys() and .values(). We are using these methods to iterate over keys and values of dictionaries separately. In below code , function `dict_iter` iterates over the keys and values of a dictionary separately.
Python3
#function to iterate over each keys and its
#corresponding values
def dict_iter(d):
#iterating over keys and values separately.
for i in d.keys():
print('KEYS: ',i)
for i in d.values():
print('VALUES: ',i)
#Main Function
if __name__ == "__main__":
d = {"Vishu":1,"Aayush":2,"Neeraj":3,"Sumit":4}
#calling function created above
dict_iter(d)
OutputKEYS: Vishu
KEYS: Aayush
KEYS: Neeraj
KEYS: Sumit
VALUES: 1
VALUES: 2
VALUES: 3
VALUES: 4
Conclusion
In Python, Dictionary is a data structure used to store data in form of key value pairs. Each created keys of a dictionary must be unique however we can duplicate the value part. Dictionary is a mutable ( which means once created we modify or update its value later on) and unordered collection of data. We can created a dictionary with dict() or with the help of curly braces.
Similar Reads
Get Key from Value in Dictionary - Python The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value. Using next() with a Generator ExpressionThis is the most efficient when we only need the first matching key. This meth
5 min read
Python dictionary values() values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain
2 min read
Iterate over a dictionary in Python In this article, we will cover How to Iterate Through a Dictionary in Python. To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys.How to Loop Through a Dictionary in PythonThere are multipl
6 min read
Python Dictionary items() method items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p
2 min read
Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to
4 min read