简体   繁体   中英

Python: Converting a List of Dictionaries to A List of Tuples?

I am interacting with an API and it returns something like this (This is a shortened version, the actual output is a couple hundred lines):

{'candles': [{'close': 319.88,
              'datetime': 1549000800000,
              'high': 324.24,
              'low': 288.7701,
              'open': 305.42,
              'volume': 128550254},
             {'close': 279.86,
              'datetime': 1551420000000,
              'high': 307.13,
              'low': 254.46,
              'open': 306.94,
              'volume': 213788920},
             {'close': 238.69,
              'datetime': 1554094800000,
              'high': 296.17,
              'low': 231.13,
              'open': 282.62,
              'volume': 230757870},
             {'close': 185.16,
              'datetime': 1556686800000,
              'high': 258.3499,
              'low': 184.1,
              'open': 238.85,
              'volume': 282598840},
             {'close': 223.46,
              'datetime': 1559365200000,
              'high': 234.74,
              'low': 176.9919,
              'open': 185.51,
              'volume': 214970560},
             {'close': 241.61,
              'datetime': 1561957200000,
              'high': 266.07,
              'low': 222.22,
              'open': 230.21,
              'volume': 199371370},
             {'close': 225.61,
              'datetime': 1564635600000,
              'high': 244.51,
              'low': 211.0,
              'open': 242.65,
              'volume': 134103755},
             {'close': 240.87,
              'datetime': 1567314000000,
              'high': 253.5,
              'low': 218.36,
              'open': 224.08,
              'volume': 136563110},
             {'close': 314.92,
              'datetime': 1569906000000,
              'high': 340.84,
              'low': 224.28,
              'open': 241.5,
              'volume': 235119040},
             {'close': 329.94,
              'datetime': 1572584400000,
              'high': 361.2,
              'low': 309.26,
              'open': 316.32,
              'volume': 157892380},
             {'close': 418.33,
              'datetime': 1575180000000,
              'high': 435.31,
              'low': 327.25,
              'open': 329.4,
              'volume': 207390450},
             {'close': 510.5,
              'datetime': 1577858400000,
              'high': 547.41,
              'low': 421.71,
              'open': 424.5,
              'volume': 236789610}],
 'empty': False,
 'symbol': 'TSLA'}

So essentially it is a dictionary of a list of dictionaries. However, I need to convert the 'candles' portion to a list of tuples containing the values like this:

[(value1, value2, value3, value4, value5, value6),
 (value1, value2, value3, value4, value5, value6),
 (value1, value2, value3, value4, value5, value6)]

Note: I need only the values in the tuples and not the keys. If anyone can help, it would be much appreciated!

You can use a list comprehension to make a tuple of the dict.values of each element in the list:

l = [
    {'key1':'value1', 'key2':'value2'},
    {'key1':'value3', 'key2':'value4'},
    {'key1':'value5', 'key2':'value6'}
]

[tuple(v) for v in map(dict.values, l)]   
# [('value1', 'value2'), ('value3', 'value4'), ('value5', 'value6')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM