I have the following code lines:
records = []
for future in futures:
records.extends(future.result())
each future returns a list.
How can I write the above code but in one liner?
records = [future.result() for future in futures]
would result in a list inside list.
I have millions of records, i would rather not flat it after creating lists inside list
records = [r for future in futures for r in future.result()]
There are many ways to do this:
itertools.chain
:records = list(itertools.chain.from_iterable(future.result() for future in futures))
itertools
consume recipe:records = collections.deque((records.extend(future.result()) for future in futures), maxlen=0)
[records.extend(future.result()) for future in futures]
. records
will now have all the required content, and you will have temporarily made a list of None
sYou could also do functools.reduce(operator.add, (future.result() for future in futures))
, but that wouldn't scale very well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With