Hilde Schneider
Hilde Schneider

Reputation: 25

How do I access a dictionary inside a list inside a list

I have a data that looks like this dict1 = [[{'num1':1, 'num2':2, 'num3':3}], [{'num1':4,'num2':5, 'num3':6}]]

How do I go inside the list where it contains the dictionary to select a specific key to display. I seem to be getting a lot if list index out of range error.

Here's my current code:

for items in dict1:
   for index in range(len(dict1)):
       print(items[index]['num1'])

Upvotes: 0

Views: 36

Answers (1)

Dan D.
Dan D.

Reputation: 8557

It's rather obvious that your dict1 is not a single-depth list, it's a list of list, so iterate it this way:

dict1 = [[{'num1':1, 'num2':2, 'num3':3}], [{'num1':4,'num2':5, 'num3':6}]]

for t in dict1:
    obj = t[0]

    for key in obj:
        print(key,obj[key])

Upvotes: 1

Related Questions