Given,
list_1 = [1,2,3,4]
list_2 = [5,6,7,8]
What is the fastest way to achieve the following in python?
l
I tested out several ways to merge two lists (see below) and came up with the following order after running each several times to normalize the cache changes (which make about a 15% difference).
import time
c = list(range(1,10000000))
c_n = list(range(10000000, 20000000))
start = time.time()
*insert method here*
print (time.time()-start)
Method 1: c.extend(c_n)
Method 2: c += c_n
Method 3: c = c + c_n
Method 4: c = [*c, *c_n]
Conclusion
Use +=
or .extend()
if you want to merge in place. They are significantly faster.