Home > Enterprise >  Nested Lists and Iteration removal
Nested Lists and Iteration removal

Time:05-19

I would like to iterate over this list

[{'quote': {'USD': {'close': 50861.552205896805,
    'high': 50982.038774035216,
    'low': 50730.75579185529,
    'market_cap': 961892403779.12,
    'open': 50881.508801727796,
    'timestamp': '2021-12-27T02:59:59.999Z',
    'volume': 20597797622.55}}},
 {'quote': {'USD': {'close': 51020.884322626065,
    'high': 51053.01734838643,
    'low': 50875.17580190205,
    'market_cap': 964908852082.23,
    'open': 51031.607414525155,
    'timestamp': '2021-12-27T04:59:59.999Z',
    'volume': 20155251492.28}}},
 {'quote': {'USD': {'close': 50961.30660215557,
    'high': 50968.54723757928,
    'low': 50787.682527382436,
    'market_cap': 963786906391.14,
    'open': 50872.72513079699,
    'timestamp': '2021-12-27T06:59:59.999Z',
    'volume': 20373929446.42}}}]

To remove the leading quote and USD bits of information ;

[{'close': 50861.552205896805,
    'high': 50982.038774035216,
    'low': 50730.75579185529,
    'market_cap': 961892403779.12,
    'open': 50881.508801727796,
    'timestamp': '2021-12-27T02:59:59.999Z',
    'volume': 20597797622.55},
 {'close': 51020.884322626065,
    'high': 51053.01734838643,
    'low': 50875.17580190205,
    'market_cap': 964908852082.23,
    'open': 51031.607414525155,
    'timestamp': '2021-12-27T04:59:59.999Z',
    'volume': 20155251492.28},
 {'close': 50961.30660215557,
    'high': 50968.54723757928,
    'low': 50787.682527382436,
    'market_cap': 963786906391.14,
    'open': 50872.72513079699,
    'timestamp': '2021-12-27T06:59:59.999Z',
    'volume': 20373929446.42}]

Any help would be greatly appreciated!

CodePudding user response:

You can use a list comprehension with lookups to remove the outer dictionaries for each entry:

[item['quote']['USD'] for item in data]

This outputs:

[
 {
  'close': 50861.552205896805,
  'high': 50982.038774035216,
  'low': 50730.75579185529,
  'market_cap': 961892403779.12,
  'open': 50881.508801727796,
  'timestamp': '2021-12-27T02:59:59.999Z',
  'volume': 20597797622.55
 },
 {
  'close': 51020.884322626065,
  'high': 51053.01734838643,
  'low': 50875.17580190205,
  'market_cap': 964908852082.23,
  'open': 51031.607414525155,
  'timestamp': '2021-12-27T04:59:59.999Z',
  'volume': 20155251492.28
 },
 {
  'close': 50961.30660215557,
  'high': 50968.54723757928,
  'low': 50787.682527382436,
  'market_cap': 963786906391.14,
  'open': 50872.72513079699,
  'timestamp': '2021-12-27T06:59:59.999Z',
  'volume': 20373929446.42
 }
]

CodePudding user response:

Just to add to other's answer.

list = [{'quote': {'USD': {'close': 50861.552205896805,
    'high': 50982.038774035216,
    'low': 50730.75579185529,
    'market_cap': 961892403779.12,
    'open': 50881.508801727796,
    'timestamp': '2021-12-27T02:59:59.999Z',
    'volume': 20597797622.55}}},
 {'quote': {'USD': {'close': 51020.884322626065,
    'high': 51053.01734838643,
    'low': 50875.17580190205,
    'market_cap': 964908852082.23,
    'open': 51031.607414525155,
    'timestamp': '2021-12-27T04:59:59.999Z',
    'volume': 20155251492.28}}},
 {'quote': {'USD': {'close': 50961.30660215557,
    'high': 50968.54723757928,
    'low': 50787.682527382436,
    'market_cap': 963786906391.14,
    'open': 50872.72513079699,
    'timestamp': '2021-12-27T06:59:59.999Z',
    'volume': 20373929446.42}}}]


data = [item['quote']['USD'] for item in list]
print(data)
  • Related