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

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

minify - Minimizing css files -

php - How to remove letter in front of the word laravel -