Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract all the values of a specific key from a list of dictionaries?

I have a list of dictionaries that all have the same structure within the list. For example:

test_data = [{'id':1, 'value':'one'}, {'id':2, 'value':'two'}, {'id':3, 'value':'three'}] 

I want to get each of the value items from each dictionary in the list:

['one', 'two', 'three'] 

I can of course iterate through the list and extract each value using a for loop:

results = [] for item in test_data:     results.append(item['value']) 

however my data set is quite large. I'm wondering if there's a faster way to this.

like image 223
James Mertz Avatar asked Aug 05 '14 21:08

James Mertz


People also ask

How do I get the value of a specific key in Python?

Python dictionary get() Method Python dictionary method get() returns a value for the given key. If key is not available then returns default value None.


2 Answers

If you just need to iterate over the values once, use the generator expression:

generator = ( item['value'] for item in test_data )  ...  for i in generator:     do_something(i) 

Another (esoteric) option might be to use map with itemgetter - it could be slightly faster than the generator expression, or not, depending on circumstances:

from operator import itemgetter  generator = map(itemgetter('value'), test_data) 

And if you absolutely need a list, a list comprehension is faster than iterated list.append, thus:

results = [ item['value'] for item in test_data ] 

You can do this:

result = map (lambda x:x['value'],test_data) 
like image 21
levi Avatar answered Sep 20 '22 21:09

levi