简体   繁体   中英

How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

I am new to Python. My question might come across as simple to experienced developers and coders but I haven't been able to find out an answer to this.

I am retrieving some data through a database query. I have succeeded in organising each row returned by query as a dictionary having three keys and a corresponding value to each key. Essentially, I have organised all rows of data returned by the query into a list of dictionaries.

The list of dictionaries looks somewhat like this:

[
    {'Date': date1, 'price': price1, 'itemnumber': number1},
    {'Date': date2, 'price': price2, 'itemnumber': number2},
    ...
]

How can I convert this list of dictionaries into three separate lists corresponding to each key ie Date , Price and itemnumber ?

Use list comprehensions :

date = [d['Date'] for d in list_of_dicts]
price = [d['price'] for d in list_of_dicts]
itemnumber = [d['itemnumber'] for d in list_of_dicts]

One could also do this in a less readable one liner:

date, price, itemnumber = zip(*[(d['Date'], d['price'], d['itemnumber']) for d in list_of_dicts])

or the same but using operator.itemgetter :

from operator import itemgetter
date, price, itemnumber = zip(*map(itemgetter('Date', 'price', 'itemnumber'), list_of_dicts))

Use map(list, ...) to turn the returned tuples into lists in the second case.

You could combine a listcomp and a dictcomp:

In [95]: ds
Out[95]: 
[{'Date': 'date1', 'itemnumber': 'number1', 'price': 'price1'},
 {'Date': 'date2', 'itemnumber': 'number2', 'price': 'price2'}]

In [96]: {key: [subdict[key] for subdict in ds] for key in ds[0]}
Out[96]: 
{'Date': ['date1', 'date2'],
 'itemnumber': ['number1', 'number2'],
 'price': ['price1', 'price2']}

Note that we have three separate lists, they're just more conveniently stored as values in a dictionary. This way, if we decide that we want to add an additional trait (say, "purchaser"), we don't need to change any of the logic.

# Defining lists
dates = []
prices = []
item_numbers = []
# Loop through list
for dictionary in your_list:
    dates.append(dictionary["Date"]) # Add the date to dates
    prices.append(dictionary["price"]) # Add the price to prices
    # Add item number to item_numbers
    item_numbers.append(dictionary["itemnumber"])
for (field,values) in [(field,
            [result.get(field) for result in results]
        ) for field in results[0].keys()]:
    # this is ugly, but I can't see any other legal way to
    # dynamically set variables.
    eval "%s=%s" % (field, repr(value))

Edited to not [illegally] modify the locals() dictionary :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM