-2

I am new to Python. I want to extract multiple lists out of the nested dictionary. I did follow this link (Get nested arrays out of a dictionary) didn't help me much.

If I have a dictionary, say:

{"alpha": {2: {1: 1.1, 2: 4.1}, 3: {1: 9.1, 3: 4.1, 6: 5.1},
           5: {1: 9.2, 3: 4.4, 6: 5.4}, 9: {1: 9.0, 3: 4.0, 6: 5.5}},
 "beta": {2: {1: 4.0, 2: 7.9}, 3: {1: 24, 3: 89, 6: 98} ,
          5: {1: 9, 3: 4, 6: 5}, 9: {1: 9.2, 3: 4.9, 6: 5.0}}}

How do I extract all these as individual lists say (alpha,beta,..), (2,3,5,9), (1,2,4,9), (1.1,4.1) etc.

When I tried I could get only the list of (alpha,beta,..) and list of values associated with alpha and beta. The list of values is again a dictionary as it is inside a list. I can't further do dict.values() as the previous operation gave me a list. Therefore, Python throws an error. How do I make list of all these values and keys individually? Like I want to make a list of decimals and the keys associated with that.

Community
  • 1
  • 1
sivasudhan
  • 303
  • 1
  • 3
  • 6
  • 1
    Please add the code of your attempts to solve the problem! – Klaus D. Oct 12 '16 at 13:21
  • I find this a bit unclear, are you trying to create lists of keys for each level of your dictionary? Is the nesting levels consistent throughout? – Lost Oct 12 '16 at 13:21
  • Are you sure that your "lets say" dictionary is made correctly? And what is `(1,2,3,9)`? And that's not lists, it's tuples. – vishes_shell Oct 12 '16 at 13:21
  • Vishes_shell, the dictionary is good as it is being used elsewhere and works good. But when I try extracting the information from the dictionaries. Sorry it is not (1,2,4,9) . it is (1,2) i.e , keys of the decimal. – sivasudhan Oct 12 '16 at 13:28
  • @Lost, sorry for the mess. yes. All I wanted to make is keys and values at different layers and label them as lists and finally plot the decimal points (values) with their corresponding keys. Yeah the pattern is same but the length of the lists might vary at the level of alpha to beta and so on. – sivasudhan Oct 12 '16 at 13:49
  • @sivasudhan ok, did Nils answer meet your needs or are you still needing help? – Lost Oct 12 '16 at 14:06
  • @Lost, Well. Nils, answer did show me a way. But then, I am working with huge modules with the data set (i.e, the dictionary) that is again huge. Hence, the assignment of lists of keys and values and calling them for plotting would be the target. I am not there yet. – sivasudhan Oct 12 '16 at 14:18
  • @sivasudhan I guess I'm unclear why you'd want to do this, which makes it hard to think of a good way to do it. If you break each level of the hierarchy into separate lists you might not be able to make the keys/values correspond anymore because dictionaries are unordered. Why can't you just plot from the dictionary? – Lost Oct 12 '16 at 15:03
  • @Lost Yeah. realized. Plotting from the dictionary was lot more easier. Thanks – sivasudhan Oct 13 '16 at 16:38

2 Answers2

2

You can get each 'layer' by accessing dict.keys() or get the layer below by accessing dict.values(). If you want to dive one level deeper you just iterate over parent.values() and get dict.keys() on each element. The last layer finally is just dict.values().

print data.keys()                                                # top level keys
>>> ['alpha', 'beta']

print [x.keys() for x in data.values()]                          # second level keys
>>> [[9, 2, 3, 5], [9, 2, 3, 5]]

print [y.keys() for x in data.values() for y in x.values()]      # third level keys
>>> [[1, 3, 6], [1, 2], [1, 3, 6], [1, 3, 6], [1, 3, 6], [1, 2], [1, 3, 6], [1, 3, 6]]

print [y.values() for x in data.values() for y in x.values()]    # third level values
>>> [[9.0, 4.0, 5.5], [1.1, 4.1], [9.1, 4.1, 5.1], [9.2, 4.4, 5.4], [9.2, 4.9, 5.0], [4.0, 7.9], [24, 89, 98], [9, 4, 5]]

Note that dicts are unordered by nature.

Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • Thanks Nils Werner. That works. But what if I want to assign these lists to some name say, u11=[1.1, 4.1] . and I want to plot them. As I am going to work with the huge chunk of data, I will have to loop and assign the lists of keys and values dynamically. Is there an easy way to assign these to lists as well ? – sivasudhan Oct 12 '16 at 14:08
  • what do you mean, assign? just write `u11 =` instead of `print`. – Nils Werner Oct 12 '16 at 14:24
  • You can always refer to a dictionary by indexes. This means that your dictionary_name['alfa'][9][6] will be 5.5. You can always create nested dictionary and it is really powerful :-) dictionary_name['alpha'][2].values() – Marco smdm Oct 12 '16 at 14:36
  • Marco, your comment helped me a lot to think my problem in new dimension. Thank you. – sivasudhan Oct 13 '16 at 16:38
0

you can convert the result of dict.keys to a list and assign to a list new_list = list(dict.keys)