list_a = []
for color in [True,False]:
for piece in range(1,7):
list_a = list_a + function(piece,color)
Here the function(piece,color)
returns a list, which I want to join and finally return the long list, can itertools.chain
be used here? because I think it might be faster. I am only displaying an example, but in my actual code the loop runs about 100,000 times, which is why I am looking for a faster method.
I'm going to answer the question you should have asked instead ;-)
This:
list_a = list_a + function(piece,color)
takes time quadratic in the number of times it's executed. Each time, a brand new list object is created, copying the entirety of the old list_a
and the new list.
So if it's executed many times, you can get a huge improvement by changing it to this:
list_a.extend(function(piece,color))
Then list_a
is extended "in place" whenever possible; under the covers, it may need to make a copy to a larger memory area from time to time, but overall the amortized time is linear in the number of times it's executed.
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