Categories
concatenation list python

How do I concatenate two lists in Python?

3246

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]

3

  • 13

    Do you want to simply append, or do you want to merge the two lists in sorted order? What output do you expect for [1,3,6] and [2,4,5]? Can we assume both sublists are already sorted (as in your example)?

    – smci

    Sep 12, 2015 at 7:51


  • 3

    …also what if the lists have duplicates e.g. [1,2,5] and [2,4,5,6]? Do you want the duplicates included, excluded, or don’t-care?

    – smci

    Jan 12, 2020 at 20:15

  • 8

    I made a youtube tutorial on 6 ways to concatenate lists if anyone finds it useful youtube.com/watch?v=O5kJ1v9XrDw

    Jun 25, 2020 at 18:48

5035

Use the + operator to combine the lists:

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

joinedlist = listone + listtwo

Output:

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

13

  • 150

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

    – Daniel F

    Apr 19, 2012 at 12:34

  • 198

    @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.

    – Daniel G

    Apr 19, 2012 at 14:51

  • 286

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

    – rickcnagy

    Jan 29, 2014 at 16:14


  • 20

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

    – MikeH

    Feb 19, 2014 at 5:01


  • 7

    @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.

    – 153957

    Apr 16, 2015 at 11:42

527

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

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]
>>> joined_list = [*l1, *l2]  # unpack both iterables in a list literal
>>> print(joined_list)
[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.

6

  • 15

    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.

    – alan

    Apr 9, 2019 at 16:15

  • 6

    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.

    – Kevin S

    May 8, 2019 at 18:16


  • 3

    The grammar nazy in me has to point out: *dictionaries

    Feb 25, 2021 at 10:39

  • It’s an excellent solution. However, it won’t work in a list comprehension.

    Jan 25 at 8:25

  • @KevinS This only works with string keys, since the ** syntax only supports string keys.

    – user16829600

    Jan 31 at 3:51

386

It’s also possible to create a generator that simply iterates over the items in both lists using itertools.chain(). 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

4

  • 11

    chain is on the slower side (but not by much) for two lists, but is the fastest solution for chaining multiple lists (n >> 2).

    – cs95

    Jun 4, 2019 at 14:10

  • @cs95 slow compared to what?

    – Moberg

    Apr 10, 2020 at 17:09

  • 1

    @Moberg Compared to other ways of concatenating lists, for reference please see my benchmarks here.

    – cs95

    May 7, 2020 at 9:00


  • @cs95 Your benchmarks uses chain to make an iterator over all the elements but the converts the result to a list. Sometimes that’s exactly what you want, but if you simply want to iterate over all the elements you can simply use the iterator from chain. That’s probably a lot faster.

    Jun 28 at 11:43