Access Element in Lists within Dictionary in Python (2 Examples)

 

In this tutorial, I’ll illustrate how to access a list element within a dictionary in the Python programming language.

The content of the tutorial looks as follows:

Let’s dive into it!

 

Exemplifying Data

We’ll use the data below as a basis for this tutorial.

data = {"list_1" : [0, 1, 3, 7, 9],            # constructing example dictionary
        "list_2" : ["apples", "bananas", "cars"],
        "list_3" : ['a', 'B', '%', '&']}

Our example data is a dictionary that contains three different lists. These lists are called list_1, list_2, and list_3.

 

Example 1: Accessing Dictionary Elements Inside a List

First, let’s see how we can access the elements of a dictionary. The elements of a dictionary (which are lists in this case) can be accessed by writing the dictionary name followed by the key name in square brackets, as shown below.

for key in data.keys():                        # iterating through the lists
    print(data[key])
# [0, 1, 3, 7, 9]
# ['apples', 'bananas', 'cars']
# ['a', 'B', '%', '&']

To access the elements inside the lists, we need to first access these lists and then access the elements inside them.

print(data["list_2"][2], data["list_1"][2])    # accessing elements inside the lists
# cars 3

The code above has extracted the element at the index position 2 of list_2 (i.e. ‘cars’) and the element at the index position 2 of list_3 (i.e. 3).

 

Example 2: Modifying Dictionary Elements Inside a List

One might also be interested in modifying the dictionary elements inside a list. In such a case, we can access the elements by selecting the respective list first and then the element, just like in the previous example. Then we can change the value of the element, as shown below.

data["list_2"][2] = "watermelons"              # modifying an element
print(data["list_2"])                          # printing the modified list
# ['apples', 'bananas', 'watermelons']

The previous syntax has changed the third item of the second list from ‘cars’ to ‘watermelons’.

 

Video, Further Resources & Summary

Do you want to learn more about lists in Python? Then you might watch the following video on my YouTube channel. In the video, I show the Python programming codes of this article.

 

The YouTube video will be added soon.

 

Furthermore, you may read some of the other tutorials on my website. I have published several other tutorials that are related to lists in Python already.

 

In summary: At this point, you should know how to manipulate list elements within a dictionary in the Python programming language. Let me know in the comments section below, in case you have any additional questions.

 

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top