33

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Naffi
  • 710
  • 1
  • 6
  • 13

6 Answers6

33

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)
phant0m
  • 16,595
  • 5
  • 50
  • 82
  • What happens if list_1 is empty (or if list_2 is empty or both)? – hepcat72 May 20 '16 at 17:55
  • 1
    @hepcat72 Nothing special, it will produce the correct result. – phant0m May 22 '16 at 06:09
  • What's the correct result? If list_1 is empty and list_2 contained a single string "a", would list contain [undef,"a"] or just ["a"]? – hepcat72 May 23 '16 at 21:44
  • 3
    `undef`? There is no such thing. `[] + ["a"] == ["a"]`. If you add a list with zero items and a list with one item, you'll end up with a list containing one item, naturally, not two. – phant0m May 24 '16 at 09:05
  • I'm a python newb. Thanks for the explanation. Just trying to figure out how python works. – hepcat72 May 24 '16 at 13:49
  • No problem, in the future, just try your own example in the Python console. "Try it and see" – phant0m May 24 '16 at 15:24
  • What if you're trying to merge half of list_1 and half of list_2 in one list called list_3? Will + still work? – AJ Gayeta Jun 13 '20 at 10:08
  • What is the result when the two lists have a common value?, will the value be repeated in the resulting list? – Gathide Aug 24 '21 at 11:07
13

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
Mohit Solanki
  • 2,122
  • 12
  • 20
13

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.

Kiran Prasad
  • 131
  • 1
  • 3
  • 3
    I tested it with python 3.8 and confirmed Kiran's results. Note that method 2 uses INPLACE_ADD bytecode instruction, meaning it operates in place (on the same memory), hence why it is faster than method 3, which uses BINARY_ADD – Almenon Jan 04 '21 at 03:16
  • @Almenon how can I see the bytecode instructions being used? – Jonathan Apr 25 '22 at 12:00
  • 1
    @Jonathan see https://docs.python.org/3/library/dis.html – Almenon Apr 26 '22 at 19:51
11

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]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
0
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]
Code Carbonate
  • 640
  • 10
  • 18
0

The fastest way appears to be:

list1 + list2

benchmark.py

#!/usr/bin/env python3
import sys
from timeit import timeit

count = 1_000_000
list_size = 1_000

l1 = list(range(list_size))
l2 = list(range(list_size))


def plus():
    return l1 + l2


def plusequal():
    lst = []
    lst += l1
    lst += l2
    return lst


def asterisk():
    return [*l1, *l2]


print('#', sys.version.replace('\n', ''))
for fn in (plus, plusequal, asterisk):
    print(fn.__qualname__, timeit(fn, number=count,
                                  globals={'l1': l1, 'l2': l2}))

Results:

$ for v in 8 9 10 11; do python3.$v benchmark.py ; done
# 3.8.16 (default, Jan 19 2023, 02:07:34) [GCC 12.2.0]
plus 2.3145008360006614
plusequal 2.388007382996875
asterisk 2.4057720839991816
# 3.9.16 (main, Jan 19 2023, 02:11:50) [GCC 12.2.0]
plus 2.410123729998304
plusequal 2.810086893001426
asterisk 2.822675076997257
# 3.10.11 (main, May  7 2023, 23:10:58) [GCC 13.1.1 20230429]
plus 2.5304620300012175
plusequal 2.8561974199983524
asterisk 2.813428355999349
# 3.11.3 (main, Jun  5 2023, 09:32:32) [GCC 13.1.1 20230429]
plus 2.473347854000167
plusequal 2.8089449279978
asterisk 2.81812875399919