0

I have a list that contains lists.

 'prices': [[1642896000000, 35180.435462830384],
            [1642982400000, 36306.409440464704],
            [1643068800000, 36774.00714224005],
            [1643155200000, 36988.928510667356],
            [1643241600000, 36870.440166930995],
            [1643328000000, 37276.839558174994],
            [1643414400000, 37852.57902803263],
            [1643479053000, 37713.12901572592]],

I trying to get a single list of the values in the 2nd part of each list, ideally ending up with 35180.43,36306.25,... rather than [35180.43,36306.25,...]

I'm trying

    resp = json.loads(response.text) # this retursn a big list
    history = []
    for i in resp:
        day = (i['prices'][0][1]) # this i think should select the value i want from the list of lists
        history.append(day) # i was hoping this would append `day` to history to do i want i want. But it doesnt

This is probably the wrong way, and I suspect there is a far easier way

Thanks

JacksWastedLife
  • 254
  • 2
  • 15

1 Answers1

2

Just use list comprehension:

prices = [[1642896000000, 35180.435462830384],
            [1642982400000, 36306.409440464704],
            [1643068800000, 36774.00714224005],
            [1643155200000, 36988.928510667356],
            [1643241600000, 36870.440166930995],
            [1643328000000, 37276.839558174994],
            [1643414400000, 37852.57902803263],
            [1643479053000, 37713.12901572592]]

prices_id = [price[0] for price in prices]
prices_value = [price[1] for price in prices]

print(prices_id)
print(prices_value)

Output:

[1642896000000, 1642982400000, 1643068800000, 1643155200000, 1643241600000, 1643328000000, 1643414400000, 1643479053000]
[35180.435462830384, 36306.409440464704, 36774.00714224005, 36988.928510667356, 36870.440166930995, 37276.839558174994, 37852.57902803263, 37713.12901572592]
Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
  • Works, but how do i make price_value just be the values rather than a list again, so i just need value1,value2. rather than [value1,value2]. is that possible? – JacksWastedLife Jan 29 '22 at 19:19
  • @JacksWastedLife could you add the desired input and output to the question? Or maybe open the new one? – Yevgeniy Kosmak Jan 29 '22 at 19:21
  • I've update the original question – JacksWastedLife Jan 29 '22 at 19:27
  • @JacksWastedLife don't know how removing the brackets could help me to understand your second request. Please provide the desired input and output. – Yevgeniy Kosmak Jan 29 '22 at 19:29
  • Errm - im not sure how to illustrate the output without removing the `[]`. from the question. the output at the moment is contained in `[]` is there a way to i to make it not in `[]` so when printed its `value1,value2,value3` – JacksWastedLife Jan 29 '22 at 19:34
  • I still don't understand what exactly do you need, but I have a guess. If you need the list items in debug output or somewhere else as items, but not a list, you can use unpacking operator `*`. `print(prices_value)` outputs `[1, 2, ...]` and `print(*prices_value)` outputs `1 2 ...`. Is that what you need? Also, if you need the string like `'1, 2, ...'` you can use `', '.join(prices_value)`. – Yevgeniy Kosmak Jan 29 '22 at 19:39