By y2k


2009-11-12 07:04:09 8 Comments

How do I concatenate two lists in Python?

Example:

listone = [1, 2, 3]
listtwo = [4, 5, 6]

Expected outcome:

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

28 comments

@Gourneau 2013-01-22 07:51:41

You could also use extend in order to add a list to the end of another one:

listone = [1,2,3]
listtwo = [4,5,6]
mergedlist = []
mergedlist.extend(listone)
mergedlist.extend(listtwo)

@Sergey 2016-08-01 16:42:22

You can use just listone.extend(listtwo) but it wil be deprecated soon

@Jim Fasarakis Hilliard 2017-10-29 10:53:58

@SergeyIvanov no it won't. list.extend isn't going anywhere.

@z33k 2018-06-04 12:49:09

Use a simple list comprehension:

joined_list = [item for list_ in [list_one, list_two] for item in list_]

It has all the advantages of the newest approach of using Additional Unpacking Generalizations - i.e. you can concatenate an arbitrary number of different iterables (for example, lists, tuples, ranges, and generators) that way - and it's not limited to Python 3.5 or later.

@sameera lakshitha 2018-12-14 21:02:17

You can use the '+' operator for concatenating two lists in Python:

>>> listone = [1,2,3]
>>> listtwo = [4,5,6]
>>>
>>> listSum = []
>>> listSum = listone + listtwo
>>> print(listSum)

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

@Vishvajit Pathak 2018-08-24 16:25:58

So there are two easy ways.

  1. Using +: It creates a new list from provided lists

Example:

In [1]: a = [1, 2, 3]

In [2]: b = [4, 5, 6]

In [3]: a + b
Out[3]: [1, 2, 3, 4, 5, 6]

In [4]: %timeit a + b
10000000 loops, best of 3: 126 ns per loop
  1. Using extend: It appends new list to existing list. That means it does not create a separate list.

Example:

In [1]: a = [1, 2, 3]

In [2]: b = [4, 5, 6]

In [3]: %timeit a.extend(b)
10000000 loops, best of 3: 91.1 ns per loop

Thus we see that out of two of most popular methods, extend is efficient.

@Tweakimp 2019-03-11 21:49:01

What if i need to add multiple lists, like a+b+c+d+e?

@lavee_singh 2015-07-07 13:30:48

If you want to merge the two lists in sorted form, you can use the merge function from the heapq library.

from heapq import merge

a = [1, 2, 4]
b = [2, 4, 6, 7]

print list(merge(a, b))

@Chris_Rands 2017-03-17 10:24:28

Note, this assumes that a and b are already sorted

@Kasrâmvd 2015-07-06 20:06:35

As a more general way for more lists you can put them within a list and use the itertools.chain.from_iterable()1 function which based on this answer is the best way for flatting a nested list:

>>> l=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> import itertools
>>> list(itertools.chain.from_iterable(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

1. Note that chain.from_iterable() is available in Python 2.6 and later. In other versions, use chain(*l).

@Robert Rossney 2009-11-12 20:01:50

It's also possible to create a generator that simply iterates over the items in both lists. This allows you to chain lists (or any iterable) together for processing without copying the items to a new list:

import itertools
for item in itertools.chain(listone, listtwo):
   # Do something with each list item

@d.putto 2012-09-25 09:37:51

This is better way because it also works with numpy array.

@zakdances 2013-03-01 00:55:23

will this work the same way: mergedList = itertools.chain(listone, listtwo) for item in mergedList:

@jfs 2014-04-30 03:38:57

@d.putto: individual item access is very slow for numpy arrays (each access requires to convert the raw memory with a C type to Python object. Vectorized operations such as np.dot() work on the C types directly without the round trip to Python and therefore fast). You could use merged = np.r_[a, b] to get concatenated numpy array.

@Tuure Laurinolli 2009-11-12 07:07:37

This is quite simple, and I think it was even shown in the tutorial:

>>> listone = [1,2,3]
>>> listtwo = [4,5,6]
>>>
>>> listone + listtwo
[1, 2, 3, 4, 5, 6]

@albus_c 2017-09-09 14:36:56

And this also works when listone and listtwo have a different number of elements ;-)

@Rarblack 2018-12-14 21:19:00

To extend a list with another one there are several methods like below:

>>> listone = [1,2,3]
>>> listome = [4,5,6]
>>>
>>> listone+listome # adding 2 list is actually extending the list
[1, 2, 3, 4, 5, 6]
>>>
>>> listone.extend(listome)
>>> listone
[1, 2, 3, 4, 5, 6]
>>>
>>> listone = [1,2,3]
>>>
>>> listone.__add__(listome)
[1, 2, 3, 4, 5, 6]

Also, for loop can also be used:

>>> for i in listome:
...     listone.append(i)
...
>>> listone
[1, 2, 3, 4, 5, 6]
>>>

@Michael Grossmann 2018-08-04 17:29:48

In Python you can concatenate two arrays of compatible dimensions with this command

numpy.concatenate([a,b])

@wonder.mice 2015-10-09 23:21:27

This question directly asks about joining two lists. However it's pretty high in search even when you are looking for a way of joining many lists (including the case when you joining zero lists).

I think the best option is to use list comprehensions:

>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> [x for xs in a for x in xs]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

You can create generators as well:

>>> map(str, (x for xs in a for x in xs))
['1', '2', '3', '4', '5', '6', '7', '8', '9']

Old Answer

Consider this more generic approach:

a = [[1,2,3], [4,5,6], [7,8,9]]
reduce(lambda c, x: c + x, a, [])

Will output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Note, this also works correctly when a is [] or [[1,2,3]].

However, this can be done more efficiently with itertools:

a = [[1,2,3], [4,5,6], [7,8,9]]
list(itertools.chain(*a))

If you don't need a list, but just an iterable, omit list().

Update

Alternative suggested by Patrick Collins in the comments could also work for you:

sum(a, [])

@Procyclinsur 2016-09-14 07:16:46

sum(a,[]) only works when "a" is a list of lists.

@Jim Fasarakis Hilliard 2017-07-24 18:00:52

Python 3 note: reduce is now in functools so you'll need to import it first.

@U9-Forward 2018-10-05 03:49:13

@Procyclinsur And the list of lists contain integers

@Ukendar Vadivel 2017-06-11 17:46:38

If you wanted a new list whilst keeping the two old lists:

def concatenate_list(listOne, listTwo):
    joinedList = []
    for i in listOne:
        joinedList.append(i)
    for j in listTwo:
        joinedList.append(j)

    sorted(joinedList)

    return joinedList

@Jim Fasarakis Hilliard 2017-07-04 12:02:01

Since you are returning, this should be in a function. (Since it's forbidden outside a function)

@py-D 2018-02-20 21:05:05

You can go for list.extend function.

l1 = [1,2,3]
l2 = [4,5,6]
l1.extend(l2)
print l1

Output:

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

@Ahsan Kamal 2018-04-20 13:00:43

More pythonic way.

@Dongkyu Choi 2018-08-08 19:34:38

What I looked for

@Akash Singh 2018-03-26 17:48:29

A really concise way to combine a list of lists is

list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
reduce(list.__add__, list_of_lists)

which gives us

[1, 2, 3, 4, 5, 6, 7, 8, 9]

@Daniel G 2009-11-12 07:07:06

You can use the + operator to combine them:

listone = [1,2,3]
listtwo = [4,5,6]

mergedlist = listone + listtwo

Output:

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

@Daniel F 2012-04-19 12:34:36

does this create a deep copy of listone and appends listtwo?

@Daniel G 2012-04-19 14:51:41

@Daniel it will create a new list with a shallow copy of the items in the first list, followed by a shallow copy of the items in the second list. Use copy.deepcopy to get deep copies of lists.

@rickcnagy 2014-01-29 16:14:49

another useful detail here: listone += listtwo results in listone == [1, 2, 3, 4, 5, 6]

@MikeH 2014-02-19 05:01:13

@br1ckb0t will that change what listone is pointing at? So:list3 = listone listone+=listtwo Is list3 changed as well?

@Guy 2014-02-19 18:11:50

@MikeH Yes it changes list3.

@rickcnagy 2014-02-20 18:55:52

it does change list3. However, if that isn't a problem, it's simpler more readable to add the two lists instead of creating a new one.

@Pygmalion 2015-04-08 18:27:39

WARNING: I don't know if this is Python3 specifics, but on certain occasions (numpy array) [1,2,3]+[4] gives [5,6,7]!!!

@153957 2015-04-16 11:42:16

@Pygmalion That is not Python3 specific, but specific to how NumPy arrays handle operators. See the answer by J.F. Sebastian in the answer by Robert Rossney for concatenating NumPy arrays.

@jpihl 2014-10-20 09:32:32

If you can't use the plus operator (+), you can uses the __add__ function:

listone = [1,2,3]
listtwo = [4,5,6]

result = list.__add__(listone, listtwo)
print(result)

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

Alternatively, if you don't like the use of dunders you can use the operator import:

import operator

listone = [1,2,3]
listtwo = [4,5,6]

result = operator.add(listone, listtwo)
print(result)

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

One could argue this is a bit more readable.

@Jim Fasarakis Hilliard 2017-07-04 12:00:19

grabbing dunders is generally not the best approach. If + is off the table, use operator.add.

@jpihl 2017-07-13 14:42:19

The looks a lot prettier, but I guess you would need import operator for that to work.

@cs01 2018-06-25 19:36:56

Why would the plus operator be unavailable?

@jpihl 2018-06-26 04:30:52

Normally it wouldn't :) but if you are doing list concatenation with the map function or want to store the add function in a variable, you can't use +.

@Jim Fasarakis Hilliard 2016-02-25 15:13:10

Python >= 3.5 alternative: [*l1, *l2]

Even though this is an old answer, another alternative has been introduced via the acceptance of PEP 448 which deserves mentioning.

The PEP, titled Additional Unpacking Generalizations, generally reduced some syntactic restrictions when using the starred * expression in Python; with it, joining two lists (applies to any iterable) can now also be done with:

>>> l1 = [1, 2, 3]
>>> l2 = [4, 5, 6]

#unpack both iterables in a list literal
>>> joinedList = [*l1, *l2]
>>> print(joinedList)
[1, 2, 3, 4, 5, 6]

This functionality was defined for Python 3.5 it hasn't been backported to previous versions in the 3.x family. In unsupported versions a SyntaxError is going to be raised.

As with the other approaches, this too creates as shallow copy of the elements in the corresponding lists.


The upside to this approach is that you really don't need lists in order to perform it, anything that is iterable will do. As stated in the PEP:

This is also useful as a more readable way of summing iterables into a list, such as my_list + list(my_tuple) + list(my_range) which is now equivalent to just [*my_list, *my_tuple, *my_range].

So while addition with + would raise a TypeError due to type mismatch:

l = [1, 2, 3]
r = range(4, 7)
res = l + r

The following won't:

res = [*l, *r]

because it will first unpack the contents of the iterables and then simply create a list from the contents.

@alan 2019-04-09 16:15:00

A nice example of the unpacking approach working on iterable types is functions that return an iterator over one of the lists you're concatenating. For example, you could reverse one of the lists you're concatenating: res = [*l1, *reversed(l2)]. Since reversed returns an iterator, res = l1 + reversed(l2) would throw an error.

@Kevin S 2019-05-08 18:16:03

It's worth noting that this is analogous to combining dictionary's in python. dict3 = {**dict1, **dict2}. Notice that we use ** to unpack the dictionary's whereas with lists we use * to unpack.

@JamesVeug 2018-02-16 03:46:31

import itertools

A = list(zip([1,3,5,7,9],[2,4,6,8,10]))
B = [1,3,5,7,9]+[2,4,6,8,10]
C = list(set([1,3,5,7,9] + [2,4,6,8,10]))

D = [1,3,5,7,9]
D.append([2,4,6,8,10])

E = [1,3,5,7,9]
E.extend([2,4,6,8,10])

F = []
for a in itertools.chain([1,3,5,7,9], [2,4,6,8,10]):
    F.append(a)


print ("A: " + str(A))
print ("B: " + str(B))
print ("C: " + str(C))
print ("D: " + str(D))
print ("E: " + str(E))
print ("F: " + str(F))

Output:

A: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
B: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
C: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
D: [1, 3, 5, 7, 9, [2, 4, 6, 8, 10]]
E: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
F: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]

@James Miller 2017-01-06 02:54:14

lst1 = [1,2]

lst2 = [3,4]

def list_combinationer(Bushisms, are_funny):

    for item in lst1:
        lst2.append(item)
        lst1n2 = sorted(lst2)
        print lst1n2

list_combinationer(lst1, lst2)

[1,2,3,4]

@U9-Forward 2018-10-05 03:54:13

Well, please do some explanation

@U9-Forward 2018-10-05 03:54:46

And Bushisms, are_funny are bad names!!!

@shiminsh 2015-08-29 18:03:13

Joining two lists in Python:

>>> a = [1, 2, 3, 4]
>>> b = [1, 4, 6, 7]
>>> c = a + b
>>> c
[1, 2, 3, 4, 1, 4, 6, 7]

If you don't want any duplication:

>>> a = [1, 2, 3, 4, 5, 6]
>>> b = [5, 6, 7, 8]
>>> c = list(set(a + b))
>>> c
[1, 2, 3, 4, 5, 6, 7, 8]

@SuperNova 2016-07-07 07:42:00

list(set(listone) | set(listtwo))

The above code, does not preserve order, removes duplicate from each list (but not from the concatenated list)

@mingxiao 2013-07-16 02:02:49

You could use the append() method defined on list objects:

mergedlist =[]
for elem in listone:
    mergedlist.append(elem)
for elem in listtwo:
    mergedlist.append(elem)

@Ryan Haining 2013-07-16 02:10:42

just so you know, if this is what you're doing in practice, this is much, much slower than the other proposed methods. see stackoverflow.com/questions/17479361/…

@Francesco Marchetti-Stasi 2015-11-02 18:59:02

As already pointed out by many, itertools.chain() is the way to go if one needs to apply exactly the same treatment to both lists. In my case, I had a label and a flag which were different from one list to the other, so I needed something slightly more complex. As it turns out, behind the scenes itertools.chain() simply does the following:

for it in iterables:
    for element in it:
        yield element

(see https://docs.python.org/2/library/itertools.html), so I took inspiration from here and wrote something along these lines:

for iterable, header, flag in ( (newList, 'New', ''), (modList, 'Modified', '-f')):
    print header + ':'
    for path in iterable:
        [...]
        command = 'cp -r' if os.path.isdir(srcPath) else 'cp'
        print >> SCRIPT , command, flag, srcPath, mergedDirPath
        [...]

The main points to understand here are that lists are just a special case of iterable, which are objects like any other; and that for ... in loops in python can work with tuple variables, so it is simple to loop on multiple variables at the same time.

@user688635 2014-07-21 00:53:24

With Python 3.3+ you can use yield from:

listone = [1,2,3]
listtwo = [4,5,6]

def merge(l1, l2):
    yield from l1
    yield from l2

>>> list(merge(listone, listtwo))
[1, 2, 3, 4, 5, 6]

Or, if you want to support an arbitrary number of iterators:

def merge(*iters):
    for it in iters:
        yield from it

>>> list(merge(listone, listtwo, 'abcd', [20, 21, 22]))
[1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 20, 21, 22]

@Radagast 2010-09-20 08:43:58

You can use sets to obtain merged list of unique values

mergedlist = list(set(listone + listtwo))

@aaronasterling 2010-09-20 08:45:54

this will lose ordering information.

@metasoarous 2012-08-21 00:28:28

True, however, it will also remove duplicates, if that's what you are interested in. List addition along would not do that.

@Natim 2013-01-29 13:12:50

What is the way to do that and keep the ordering information?

@Natim 2013-01-29 13:13:40

Better than listone + [x for x in listtwo if x not in listone]

@Jose Salvatierra 2013-07-23 12:24:46

If I had a list of lists, such as this one: [[0, 5], [1, 10], [0, 7], [3, 5]] How would you merge them to avoid duplicates in the key (first value in each sub-list), but if they are duplicates, end up with the sum of the second values? Like so: [[0, 12], [1, 10], [3, 5]] Thanks

@andrewb 2013-08-17 02:06:54

@jslvtr That may be worth a separate question

@alfasin 2014-04-27 04:07:50

+1 IMHO this is the correct way to "merge" (union) lists while the "approved" answer describes how to combine/add lists (multiset)

@SethMMorton 2016-12-15 20:11:24

If you care about maintaining input order, then import collections; mergedlist = list(collections.OrderedDict.fromkeys(listone + listtwo)) will do the trick.

@Mr Shark 2013-11-07 18:04:54

If you need to merge two ordered lists with complicated sorting rules, you might have to roll it yourself like in the following code (using a simple sorting rule for readability :-) ).

list1 = [1,2,5]
list2 = [2,3,4]
newlist = []

while list1 and list2:
    if list1[0] == list2[0]:
        newlist.append(list1.pop(0))
        list2.pop(0)
    elif list1[0] < list2[0]:
        newlist.append(list1.pop(0))
    else:
        newlist.append(list2.pop(0))

if list1:
    newlist.extend(list1)
if list2:
    newlist.extend(list2)

assert(newlist == [1, 2, 3, 4, 5])

@Amyth 2013-03-05 09:15:56

You could simply use the + or += operator as follows:

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

c = a + b

Or:

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

c += (a + b)

Also, if you want the values in the merged list to be unique you can do:

c = list(set(a + b))

@Dariusz Walczak 2013-02-05 05:01:34

It's worth noting that the itertools.chain function accepts variable number of arguments:

>>> l1 = ['a']; l2 = ['b', 'c']; l3 = ['d', 'e', 'f']
>>> [i for i in itertools.chain(l1, l2)]
['a', 'b', 'c']
>>> [i for i in itertools.chain(l1, l2, l3)]
['a', 'b', 'c', 'd', 'e', 'f']

If an iterable (tuple, list, generator, etc.) is the input, the from_iterable class method may be used:

>>> il = [['a'], ['b', 'c'], ['d', 'e', 'f']]
>>> [i for i in itertools.chain.from_iterable(il)]
['a', 'b', 'c', 'd', 'e', 'f']

Related Questions

Sponsored Content

58 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

30 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2214894 View
  • 3237 Score
  • 30 Answer
  • Tags:   python list

22 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3214222 View
  • 3475 Score
  • 22 Answer
  • Tags:   python directory

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

38 Answered Questions

[SOLVED] What does the "yield" keyword do?

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3242067 View
  • 2675 Score
  • 29 Answer
  • Tags:   python list indexing

39 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

17 Answered Questions

[SOLVED] How to make a chain of function decorators?

35 Answered Questions

[SOLVED] How to read a file line-by-line into a list?

13 Answered Questions

[SOLVED] Getting the last element of a list in Python

  • 2009-05-30 19:28:53
  • Janusz
  • 1625360 View
  • 1689 Score
  • 13 Answer
  • Tags:   python list indexing

Sponsored Content