Access Dictionary within List in Python (Example)

 

In this post, I’ll illustrate how to access a dictionary within a list in Python programming.

Table of contents:

So without further additions, let’s get started.

 

Example Data

First, we need to construct our example data, which is a list of two dictionaries where each dictionary has two key-value pairs, as shown below.

data = [{"five" : 5, "seven" : 7}, {"four" : 4, "six" : 6}]  # constructing example list

 

Example: Access Dictionary within List in Python

Let’s solve the problem step by step. First, we need to access the elements of the list, which are the dictionaries in this case, by iterating through the list. For the output, see below.

for some_dict in range(len(data)):                           # iterating through the list
    print(data[some_dict])
# {'five': 5, 'seven': 7}
# {'four': 4, 'six': 6}

To access the elements inside the dictionaries, we need to access the dictionary first, then use the keys of the dictionaries to reach their elements.

print(data[0]["five"], data[1]["six"])                       # accessing inside the dictionaries
# 5 6

In this case, two elements are printed: the first one is the element labeled with the key “five” in the first dictionary, and the second one is the element labeled with the key “six” in the second dictionary.

 

Video, Further Resources & Summary

In case you need further explanations on the Python programming syntax of this post, I recommend watching the following video on my YouTube channel. In the video instruction, I’m illustrating the Python code of this article.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at the related posts on www.statisticsglobe.com. You can find a selection of articles below:

 

In this tutorial, I have demonstrated how to use a dictionary within lists in Python programming. If you have further questions, don’t hesitate to let me know in the comments.

 

Ö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.


6 Comments. Leave new

  • Hello! How I can to find your first lessons of Python? I want to leorn python but I don’t know how begin it. Thank…

    Reply
  • There is big difference between R and Python?

    Reply
    • Hello Zulfiya,

      In R, there isn’t a native “dictionary” type as there is in Python (where it’s an actual dict). However, you can use lists in R to create a similar structure, where elements are named and you can access them using those names, much like keys in a Python dictionary.

      Suppose you have a list of lists, where each inner list has named elements (this structure is analogous to a list of dictionaries in Python). You can access the elements of the inner lists (the “dictionary” entries) using the $ operator or double square brackets [[ ]].

      Here’s an example to illustrate:

      # Create a list of lists with named elements
      list_of_lists <- list(
        list(name = "Alice", age = 25, salary = 50000),
        list(name = "Bob", age = 30, salary = 55000),
        list(name = "Charlie", age = 28, salary = 60000)
      )
       
      # Accessing elements
      # To get the name of the first 'dictionary' (list)
      print(list_of_lists[[1]]$name)

      Best,
      Cansu

      Reply

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