I have a list of Objects and each object has inside it a list of other object type. I want to extract those lists and create a new list of the other object.
List1:[Obj1, Obj2, Obj3]
Obj1.myList = [O1, O2, O3]
Obj2.myList = [O4, O5, O6]
Obj3.myList = [O7, O8, O9]
I need this:
L = [O1, O2, O3, O4, ...., O9];
I tried extend()
and reduce()
but didn't work
bigList = reduce(lambda acc, slice: acc.extend(slice.coresetPoints.points), self.stack, [])
P.S.
Looking for python flatten a list of list didn't help as I got a list of lists of other object.
using
itertools.chain
(or even better in that caseitertools.chain.from_iterable
as niemmi noted) which avoids creating temporary lists and usingextend
or (much clearer and slightly faster):
small reproduceable test:
result:
(all recipes to flatten a list of lists: How to make a flat list out of list of lists?)