Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list out of nested list in python

Tags:

python

list

I am getting list out of a nested list.

list_of_data = [{'id':99,
                 'rocketship':{'price':[10, 10, 10, 10, 10], 
                               'ytd':[1, 1, 1.05, 1.1, 1.18]}},
                {'id':898,
                 'rocketship':{'price':[10, 10, 10, 10, 10], 
                               'ytd':[1, 1, 1.05, 1.1, 1.18]}},
                {'id':903,
                 'rocketship':{'price':[20, 20, 20, 10, 10], 
                               'ytd':[1, 1, 1.05, 1.1, 1.18]}},
                {'id':999,
                 'rocketship':{'price':[20, 20, 20, 10, 10], 
                               'ytd':[1, 3, 4.05, 1.1, 1.18]}},
                ]

price, ytd = map(list, zip(*((list_of_data[i]['rocketship']['price'], list_of_data[i]['rocketship']['ytd']) for i in range(0, len(list_of_data)))))

My expected output is below (But, I am getting something different):

price = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 10, 10, 20, 20, 20, 10, 10]

ytd = [1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 3, 4.05, 1.1, 1.18]

But, I am getting this:
price
Out[19]: 
[[10, 10, 10, 10, 10],
 [10, 10, 10, 10, 10],
 [20, 20, 20, 10, 10],
 [20, 20, 20, 10, 10]]

Expected output:

price = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 10, 10, 20, 20, 20, 10, 10]

ytd = [1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 3, 4.05, 1.1, 1.18]
like image 877
Anjat Avatar asked Mar 30 '26 04:03

Anjat


2 Answers

try this:

update

Thanks @shawn caza performance test for 100000 loops:

shawncaza answer: 0.10945558547973633 seconds

my answer with get method : 0.1443953514099121 seconds

my answer with square bracket method : 0.10936307907104492 seconds

list_of_data = [{'id': 99,
             'rocketship': {'price': [10, 10, 10, 10, 10],
                            'ytd': [1, 1, 1.05, 1.1, 1.18]}},
            {'id': 898,
             'rocketship': {'price': [10, 10, 10, 10, 10],
                            'ytd': [1, 1, 1.05, 1.1, 1.18]}},
            {'id': 903,
             'rocketship': {'price': [20, 20, 20, 10, 10],
                            'ytd': [1, 1, 1.05, 1.1, 1.18]}},
            {'id': 999,
             'rocketship': {'price': [20, 20, 20, 10, 10],
                            'ytd': [1, 3, 4.05, 1.1, 1.18]}},
            ]
price = []
ytd = []
for i in list_of_data:
    price.extend(i['rocketship']['price'])
    ytd.extend(i['rocketship']['ytd'])
print(price)
print(ytd)

>>> [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 10, 10, 20, 20, 20, 10, 10]
>>> [1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 3, 4.05, 1.1, 1.18]
like image 188
Ramesh Avatar answered Apr 02 '26 02:04

Ramesh


Using list comprehension:

price, ytd = [i for item in list_of_data for i in item["rocketship"]["price"]],
             [i for item in list_of_data for i in item["rocketship"]["ytd"]]

Output

price: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 10, 10, 20, 20, 20, 10, 10] 

ytd: [1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 1, 1.05, 1.1, 1.18, 1, 3, 4.05, 1.1, 1.18]
like image 20
Abdo Sabry Avatar answered Apr 02 '26 04:04

Abdo Sabry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!