I have a list - list_of_objects = [<obj1>,<obj2>]. Each object has an attribute <ob1j>.content. Each content attribute holds a list of dictionaries [{"key":"value"}, {"key":"value"}]. How do I use list comprehension to "unpack" these dictionaries into a single list? Example that doesn't work:
list_of_objects = [<obj1>,<obj2>]
<ob1j>.content
content
[{"key":"value"}, {"key":"value"}]
list_of_dictionaries = [dict for obj in list_of_objects for item in obj.content]
Basically I want to turn the below loop that works into a comprehension:
for obj in list_of_objects: new_list.extend(obj.content)
Calling dir on the object gives you back all the attributes of that object, including python special attributes.You can always filter out the special methods by using a list comprehension.
2.1m questions
2.1m answers
60 comments
56.9k users