Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Loop Dictionary Items


Loop Dictionary Items

Use for loop statement to iterate the dictionary items in python.

While looping the dictionary using for loop statement, it returns all keys of the dictionary in iteration.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

for i in company_dictionary:
    print(i)
name
country
start_year

Here, we are iterating the company_dictionary by using the for loop statement and printing the items, so as you can see in the output, all the dictionary keys are displayed.


You can also fetch dictionary item values by referring key while iterating the loop.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

for i in company_dictionary:
    print(company_dictionary[i])
Google
USA
1995

In the output, you can see we printed all values of company_dictionary while iterating the for loop statement.


Using the keys() function, you can retrieve all dictionary keys.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

for i in company_dictionary.keys():
    print(i)
name
country
start_year

In the above example, we fetched all keys of company_dictionary using the keys() function and printed them while iterating the for loop statement.


Using the values() function, you can retrieve all dictionary values.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

for i in company_dictionary.values():
    print(i)
Google
USA
1995

We fetched all values of company_dictionary using the values() function and printed them while iterating the for loop statement.


Using the items() function, you can retrieve all keys and values of the dictionary.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

for key, value in company_dictionary.items():
    print(key, value)
name Google
country USA
start_year 1995

In the above example, we fetched all keys and values of company_dictionary using the items() function and printed them while iterating the for loop statement.