简体   繁体   中英

What is the fastest way to merge two lists in python?

Given,

list_1 = [1,2,3,4]
list_2 = [5,6,7,8]

What is the fastest way to achieve the following in python?

list = [1,2,3,4,5,6,7,8]

Please note that there can be many ways to merge two lists in python.
I am looking for the most time-efficient way.

I tried the following and here is my understanding.

CODE

import time

c = list(range(1,10000000))
c_n = list(range(10000000, 20000000))

start = time.time()
c = c+c_n
print len(c)
print time.time() - start

c = list(range(1,10000000))
start = time.time()
for i in c_n:
    c.append(i)
print len(c)
print time.time() - start

c = list(range(1,10000000))
start = time.time()
c.extend(c_n)
print len(c)
print time.time() - start

OUTPUT

19999999
0.125061035156
19999999
1.02858018875
19999999
0.03928399086

So, if someone does not bother reusing list_1/list_2 in the question then extend is the way to go. On the other hand, "+" is the fastest way.

I am not sure about other options though.

You can just use concatenation:

list = list_1 + list_2

If you don't need to keep list_1 around, you can just modify it:

list_1.extend(list_2)

list_1 + list_2 does it. Example -

>>> list_1 = [1,2,3,4]
>>> list_2 = [5,6,7,8]
>>> list_1 + list_2
[1, 2, 3, 4, 5, 6, 7, 8]

If you are using python 3, there is one more way to do this and a little bit faster (tested only on python 3.7)

[*list1, *list2]

Benchmark

from timeit import timeit
x = list(range(10000))
y = list(x)

def one():
    x + y

def two():
    [*x, *y]

print(timeit(one, number=1000, globals={'x':x, 'y': y}))
print(timeit(two, number=1000, globals={'x':x, 'y': y}))
0.10456193100253586
0.09631731400440913

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)

    • Representative result: 0.11861872673034668
  • Method 2: c += c_n

    • Representative result: 0.10558319091796875
  • Method 3: c = c + c_n

    • Representative result: 0.25804924964904785
  • Method 4: c = [*c, *c_n]

    • Representative result: 0.22019600868225098

Conclusion Use += or .extend() if you want to merge in place. They are significantly faster.

a=[1,2,3]
b=[4,5,6]

c=a+b
print(c)

OUTPUT:

 >>> [1, 2, 3, 4, 5, 6]

In the above code "+" operator is used to concatenate the 2 lists into a single list.

ANOTHER SOLUTION:

 a=[1,2,3]
 b=[4,5,6]
 c=[] #Empty list in which we are going to append the values of list (a) and (b)

 for i in a:
     c.append(i)
 for j in b:
     c.append(j)

 print(c)

OUTPUT:

>>> [1, 2, 3, 4, 5, 6]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM