How to use keys() In Python

After iterating to a list the keys from the dictionary can further be extracted using the keys() function.

Example: Extracting key values

Python3




# Create a list of dictionaries
languages = [
    {
        "Python": "Machine Learning",
        "R": "Machine learning",
    },
    {
        "Python": "Web development",
        "Java Script": "Web Development",
        "HTML": "Web Development"
    },
    {
        "C++": "Game Development",
        "Python": "Game Development"
    },
    {
        "Java": "App Development",
        "Kotlin": "App Development"
    }
]
 
# iterate over the list
for i in languages:
   
    # now i is a dict, now we see the keys
    # of the dict
    for key in i.keys():
       
        # print every key of each dict
        print(key)
 
    print("-------------")


Output:

Python

R

————-

Python

Java Script

HTML

————-

C++

Python

————-

Java

Kotlin

————-

Iterate through list of dictionaries in Python

In this article, we will learn how to iterate through a list of dictionaries. 

List of dictionaries in use:

[{‘Python’: ‘Machine Learning’, ‘R’: ‘Machine learning’}, 

{‘Python’: ‘Web development’, ‘Java Script’: ‘Web Development’, ‘HTML’: ‘Web Development’}, 

{‘C++’: ‘Game Development’, ‘Python’: ‘Game Development’}, {‘Java’: ‘App Development’, ‘Kotlin’: ‘App Development’}]

Similar Reads

Method 1: Using indexing

This is a direct method, where list elements are extracted using just the index....

Method 2: Using keys()

...

Method 3: Using list comprehension

...

Contact Us