-1

So I've found these two resources: Iterating over list of dictionaries and Nested dictionary comprehension python

but I'm new to python and am having trouble understanding nested structures within different structures (nested dictionaries within a list, or nested lists within a dictionary.

For instance, if I'm trying to get the dictionary within camera (so the key-item pairs of "color": "black", "res": 16 and then append that to a list.

Thanks much!

*updating below to append to a list.

nested_lst3 = [{"camera":{"color": "black", "res": 16}, "phone":1}, {"car":{"amount": 1, "year": 2017, "color": "red"}, "van":0}]
def get_values(d):
  for i in d:
    for key in d[i]:
        l.append(key)
  return(l)

#or this

def get_values(d):
  for i in d:
    for key, values in d[i]:
      if type(values) is dict:
        get_values(values)
      else:
        print(key, ":", value)

get_values(nested_lst3)

James
  • 459
  • 2
  • 14
  • 1
    Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Teach me these language features" is off-topic for Stack Overflow. Stack Overflow is not intended to replace existing tutorials and documentation. You simply work through each layer of nesting, one at a time. – Prune Feb 28 '21 at 04:53
  • What in your opinion is Stack Overflow for? I've created a minimal reproducible example for my question. I think my question is pretty clear. I've looked at resources here: https://holypython.com/intermediate-python-lessons/lesson-6-nested-data-in-python/ and here https://towardsdatascience.com/15-things-you-should-know-about-dictionaries-in-python-44c55e75405c and am failing to understand something, so I'm not sure what you have in mind for SO, but my understanding was that that was what this site is for. – James Feb 28 '21 at 05:26
  • The big question is, what do you want to do with this? Your subdictionaries all have different keys, so there's no obvious way to collate them. That makes iteration hard to define. You can get the color of the camera through nested_lst3[0]['camera']['color'], but that's not your question. – Tim Roberts Feb 28 '21 at 05:40
  • What I'm suggesting is that your data structure is not useful. Let's figure out what you want to DO, and that will dictate how you store it for easy access. – Tim Roberts Feb 28 '21 at 05:41
  • Thanks, so I understand what you're saying re the data structure not being useful and this is good to note. Let's say that I wanted to append the values to a list, which I will update in my question. My main language is R, so I don't work often with for loops, and in a grad Python course I'm taking, not everything seems embedded in the most practical way of solving something. – James Feb 28 '21 at 05:49
  • It's not merely my opinion -- it's the stated purpose of the site: an archive of useful questions and answers ... for professional and enthusiast programmers. Tutorial questions are specifically off topic. – Prune Feb 28 '21 at 05:54
  • I'm not sure where it says that tutorial questions are off topic, and I'm not sure who has delegated this question a "tutorial question." I do know that back and forths like this are discouraged on the site, so I'll stop it here. Not everyone is as advanced as you are (which is pretty obvious from your 60K rating); so what is not useful to one person, may very well be useful to another. Thanks for being a part of this community. – James Feb 28 '21 at 06:09

1 Answers1

1

if you want a function that will only get {"color":"black", "res":16} then you could do this:

def get_values(d):
    s = {}
    for i in range(len(d)):
        temp = d[i]
        s = temp[list(temp.keys())[0]]
        break
    return s

What this does is that it goes through the nested list variable. Because you need to have a key to be able to access a value in a dictionary, you could go one by one in the list and then on each iteration take the first key, `temp = d[i]'

In this approach, it is assumed that we already know the first value in the dictionary is going to be another dictionary. So we just take the key,( list(temp.keys())[0] makes a list of the keys present in the dictionary) and just take the first one.

I added a break statement since you only want the first nested dictionary, but if you were to take out the break, it will retrieve every nested dictionary (assuming they are all located in the first value position of the nested dictionary. EDIT: if you want it to return an array with the dictionary in it:

def get_values(d):
        s = []
        for i in range(len(d)):
            temp = d[i]
            s.append(temp[list(temp.keys())[0]])
            break
        return s
Cristian
  • 303
  • 2
  • 8
  • Perfect! This is helpful. So when you write "break statement since you only want the first nested dictionary" if I had another nested dictionary, would I not only have to take out the break but add in another for loop? – James Feb 28 '21 at 06:04