How to use indexing In Python

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

Syntax:

list[index]

Example:

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"
    }
]
 
 
print(languages[0])
print(languages[1])
print(languages[2])
print(languages[3])


Output:

{‘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’}

After using indexing to particular dictionaries, now we can treat each item of the list as a dictionary,

Example: Extracting values from a particular dictionary

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"
    }
]
 
for key, val in languages[0].items():
    print("{} : {}".format(key, val))


Output:

Python : Machine Learning

R : Machine learning

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