Joining inner members inside a list of objects in Python -
this question has answer here:
i have following array:
[ {"name": "abc", "age": 10}, {"name": "xyz", "age": 12}, {"name": "def", "age": 15} ]
i want create following array out of it:
["abc","xyz","def"]
ie take name field out of each object. there easier way other through loop?
you have error in dictionary syntax, i'm assuming wanted use string keys.
the issue solved using list comprehensions:
data = [ {'name': 'abc', 'age': 10}, {'name': 'xyz', 'age': 12}, {'name': 'def', 'age': 15} ] print([item['name'] item in data]) #=> ['abc', 'xyz', 'def']
you use map
avoid using loops, it's not pythonic:
map(lambda i: i['name'], data)
Comments
Post a Comment