-2

Essentially I have a dictionary that looks like this.

dictionary = { 'key': [ { 'key': 'value', etc.. } etc...] }

I want a list of just 'value'. I'm quite new, so how can I go about this? Thanks.

    dictionary = {
                   "candles":[
                              { 
                               "open": 78.21,
                               "high": 78.28,
                               "low": 78.12,
                               "close": 78.2,
                               "volume": 16417,
                               "datetime": 1620385200000
                               },
                               {
                                "open": 78.19,
                                "high": 78.26,
                                "low": 78.17,
                                "close": 78.2,
                                "volume": 5928,
                                "datetime": 1620387000000
                                }
                             [
                 }
mospira
  • 21
  • 4

2 Answers2

1

Perhaps :

values = []
for key in dictionary:
    for nested_dict in dictionary[key]:
        values += nested_dict.values()

values will be:

[78.21, 78.28, 78.12, 78.2, 16417, 1620385200000, 78.19, 78.26, 78.17, 78.2, 5928, 1620387000000]
Charles Dupont
  • 995
  • 3
  • 9
  • Can also do `values.extend(nested_dict.values())` to [extend a list](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) – Gino Mempin May 11 '21 at 02:27
1

You can use a nested list comprehension.

res = [val for l in dictionary.values() for d in l for val in d.values()]

Demo

Unmitigated
  • 76,500
  • 11
  • 62
  • 80