-2

looking for suggestion on how to flatten this lists in python

sample:

[['BTCUSDT', [[1640995200000, '46216.93000000', '47954.63000000', '46208.37000000', '47722.65000000', '19604.46325000', 1641081599999, '924155159.58348650', 714899, '9942.36679000', '468738711.79010650', '0']]], ['ETHUSDT', [[1640995200000, '3676.22000000', '3776.45000000', '3673.46000000', '3765.54000000', '154143.89680000', 1641081599999, '574718328.45274400', 408666, '76937.97980000', '286855413.58892400', '0']]], ['BNBUSDT', [[1640995200000, '511.50000000', '527.90000000', '511.40000000', '527.30000000', '446171.54100000', 1641081599999, '231696121.29960000', 329538, '228746.06000000', '118795531.09300000', '0']]], ['BCCUSDT', []], ['NEOUSDT', [[1640995200000, '25.67000000', '26.54000000', '25.67000000', '26.38000000', '212078.03000000', 1641081599999, '5557995.20740000', 17592, '93852.59000000', '2459402.23260000', '0']]], ....cut portion...]

It should look like this for the first item: [['BTCUSDT', 1640995200000, '46216.93000000', '47954.63000000', '46208.37000000', '47722.65000000', '19604.46325000', 1641081599999, '924155159.58348650', 714899, '9942.36679000', '468738711.79010650', '0']....]

but I kind of also want to just retain only the first 7 items in the list, deleting the others, kind of just like this:

[['BTCUSDT', 1640995200000, '46216.93000000', '47954.63000000', '46208.37000000', '47722.65000000', '19604.46325000'].....next...]

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jomsky99
  • 3
  • 1
  • is [How to make a flat list out of a list of lists](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) what you want? – Robin Dillen Jan 04 '22 at 15:38

1 Answers1

0

Unclear how you plan on using the data, but I don't think mixing the symbol with the timestamps/values will help.

In any case, you can get the first element of each list, then the first seven of the next, then append those to a new list

result = []
for d in data:

  symbol = d[0]
  elements = d[1][0][:7] if d[1] else []
  result.append([symbol, elements])

print(result)

Output (formatted)

[['BTCUSDT',
  [1640995200000,
   '46216.93000000',
   '47954.63000000',
   '46208.37000000',
   '47722.65000000',
   '19604.46325000',
   1641081599999]],
 ['ETHUSDT',
  [1640995200000,
   '3676.22000000',
   '3776.45000000',
   '3673.46000000',
   '3765.54000000',
   '154143.89680000',
   1641081599999]],
 ['BNBUSDT',
  [1640995200000,
   '511.50000000',
   '527.90000000',
   '511.40000000',
   '527.30000000',
   '446171.54100000',
   1641081599999]],
 ['BCCUSDT', []],
 ['NEOUSDT',
  [1640995200000,
   '25.67000000',
   '26.54000000',
   '25.67000000',
   '26.38000000',
   '212078.03000000',
   1641081599999]]]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I want to remove the nested lists. The symbol and data should be together. I want to export them to csv file – Jomsky99 Jan 04 '22 at 16:30
  • Okay, then make another list variable within the for loop, then call `.append(symbol)` and `.extend(elements)` separately, then append that list to the outer result list. Keep in mind that [JSON would be a better format than CSV for such data](https://jsonlines.org/examples/) – OneCricketeer Jan 04 '22 at 19:27