python - generate list from values of certain field in list of objects -
how generate list of values of field of objects in list?
given list of objects:
[ {name: "joe", group: 1}, {name: "kirk", group: 2}, {name: "bob", group: 1}]
i want generate list of name field values:
["joe", "kirk", "bob"]
the built-in filter()
function seems come close, return entire objects themselves.
i'd clean, 1 line solution such as:
filterlikefunc(function(obj){return obj.name}, mylist)
sorry, know that's c syntax.
using list comprehension approach get:
objects = [{'group': 1, 'name': 'joe'}, {'group': 2, 'name': 'kirk'}, {'group': 1, 'name': 'bob'}] names = [i["name"] in objects]
for intro list comprehensions, see https://docs.python.org/2/tutorial/datastructures.html
Comments
Post a Comment