Open In App

Python Iterate Dictionary Key, Value

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

KEYS: 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)


Output

KEYS: 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)


Output

KEYS:  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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads