Search code examples
pythonarraysjsonlistextract

Python: How to extract a List thats inside an array, which is inside a list?


Been trying to figure this out everyday for a week...

I am receiving data, then using json.loads and variable-ing it. Here is the print output that I cant seem to further variable-ize without error codes:

[548, {'a': ['2364.66000', 16, '16.94656538'], 'b': ['2364.65000', 8, '8.45377850'], 'c': ['2364.66000', '0.35264111'], 'v': ['494.53876892', '54702.28957251'], 'p': ['2363.99238', '2359.02527'], 't': [404, 31739], 'l': ['2355.99000', '2258.59000'], 'h': ['2375.91000', '2453.03000'], 'o': ['2369.82000', '2338.83000']}, 'ticker', 'somerandontextcharacters']

Trying to assign one or more of these boldly-specific numbers to variables, you know, so I can have my way with them.

P.S. x = [548,{thewholething-etc...]

P.S.S. the data is received from a websocket connection response using .recv command, then json.loads'ed. Some of the printed outputs are in {etc,etc,} but some are in the form of [etc,{etc,etc,}]


Solution

  • It's pretty easy actually:

    test = [{'a' : [1,2,3], 'b': [4,5,6]}] # this is just an example
    
    var1 = test[0]['a'][1] # should be 2
    var2 = test[0]['b'][2] # should be 6
    
    print(var1)
    print(var2)
    

    Output:

    2
    6