Reputation: 33
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 i.e. Date
, Price
and itemnumber
?
Upvotes: 3
Views: 690
Reputation: 78554
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.
Upvotes: 6
Reputation: 1812
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 :(
Upvotes: 0
Reputation: 353369
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.
Upvotes: 2
Reputation: 841
# 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"])
Upvotes: 1