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

  • Last Update :
  • Techknowledgy :

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))

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']
}
# 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))

Suggestion : 2

Last Updated : 09 Aug, 2022

Example

Input: {
   'a': 'akshat',
   'b': 'bhuvan',
   'c': 'chandan'
}
Output:
   keys: ['a', 'b', 'c']
values: ['asshat', 'bhuvan', 'chandan']

Output:

intial_dictionary {
   'a': 'akshat',
   'b': 'bhuvan',
   'c': 'chandan'
}
keys: dict_keys(['a', 'b', 'c'])
values: dict_values(['akshat', 'bhuvan', 'chandan'])

intial_dictionary {
   'a': 'akshat',
   'b': 'bhuvan',
   'c': 'chandan'
}
keys: ['a', 'b', 'c']
values: ['akshat', 'bhuvan', 'chandan']

intial_dictionary {
   'a': 'akshat',
   'b': 'bhuvan',
   'c': 'chandan'
}
keys: ['a', 'b', 'c']
values: ['akshat', 'bhuvan', 'chandan']

Suggestion : 3

In the following program, we create a list of length 3, where all the three elements are of type dict.,In the following program, we shall append a dictionary to the list of dictionaries.,In the following program, we shall print some of the values of dictionaries in list using keys.,In this tutorial, we will learn how to create a list of dictionaries, how to access them, how to append a dictionary to list and how to modify them.

Python Program

myList = [{
      'foo': 12,
      'bar': 14
   },
   {
      'moo': 52,
      'car': 641
   },
   {
      'doo': 6,
      'tar': 84
   }
]

print(myList)

Output

[{
   'foo': 12,
   'bar': 14
}, {
   'moo': 52,
   'car': 641
}, {
   'doo': 6,
   'tar': 84
}]