returning values of a particular key from a dictionary

John O'Hagan research at johnohagan.com
Fri May 1 11:03:50 CEST 2009


On Fri, 1 May 2009, Saurabh wrote:
> arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'})
> print foo(arr, 'y')
> ['a','b','c']
>
> I can write the function foo to return ['a','b','c'].
> Is there some 'automatic'/built-in way to get this in Python ?

List comprehension:

[i['y'] for i in arr]


Regards,

John



More information about the Python-list mailing list