Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Itertools instead of nested loops

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.

like image 549
Vajjhala Avatar asked Dec 18 '22 08:12

Vajjhala


1 Answers

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.

like image 161
Tim Peters Avatar answered Dec 20 '22 20:12

Tim Peters