9

In python, how do I concatenate 3 lists using a list comprehension?

Have:

    list1 = [1,2,3,4]
    list2 = [5,6,7,8]
    list3 = [9,10,11,12]

Want:

    allList = [1,2,3,4,5,6,7,8,9,10,11,12]

I tried using a list comprehension, but I'm not very good at them yet. These are what I have tried:

    allList = [n for n in list1 for n in list2 for n in list3 ]

this was a bad idea, obviously and yielded len(list1)*len(list2)*len(list3) worth of values. Oops. So I tried this:

    allList = [n for n in list1, list2, list3]

but that gave me allList = [list1, list 2, list3] (3 lists of lists)

I know you can concatenate using the + operator (as in x = list1 + list2 + list3)but how do you do this using a simple list comprehension?

There is a similar question here: Concatenate 3 lists of words , but that's for C#.

Community
  • 1
  • 1
TheProletariat
  • 916
  • 2
  • 11
  • 23
  • I can't see the benefits of doing that :S – jabaldonedo Aug 07 '13 at 21:51
  • Can you explain what benefit you think you're getting from using a list comprehension? I think you must have some misconception regarding what they do. – Peter DeGlopper Aug 07 '13 at 21:52
  • Possible duplicate of [Making a flat list out of list of lists in Python](https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python) – Justin Sep 26 '18 at 20:04

6 Answers6

15

A better solution is to use itertools.chain instead of addition. That way, instead of creating the intermediate list list1 + list2, and then another intermediate list list1 + list2 + list3, you just create the final list with no intermediates:

allList = [x for x in itertools.chain(list1, list2, list3)]

However, an empty list comprehension like this is pretty silly; just use the list function to turn any arbitrary iterable into a list:

allList = list(itertools.chain(list1, list2, list3))

Or, even better… if the only reason you need this is to loop over it, just leave it as an iterator:

for thing in itertools.chain(list1, list2, list3):
    do_stuff(thing)

While we're at it, the "similar question" you linked to is actually a very different, and more complicated, question. But, because itertools is so cool, it's still a one-liner in Python:

itertools.product(list1, list2, list3)

Or, if you want to print it out in the format specified by that question:

print('\n'.join(map(' '.join, itertools.product(list1, list2, list3))))
Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
14

Here are some options:

>>> sum([list1, list2, list3], [])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

>>> list1 + list2 + list3
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

With comprehension: (it's really not necessary)

>>> [x for x in  sum([list1, list2, list3], [])]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
zs2020
  • 53,766
  • 29
  • 154
  • 219
  • Thanks for `sum([list1, list2, list3], [])`. I wanted to avoid using itertools just for one line in my code. Not that avoiding was a priority. This help. Area there any caveats to this? Is itertools faster? – shahensha Nov 20 '20 at 03:19
  • awesome [x for x in sum([list1, list2, list3], [])] – Beqa Bukhradze Dec 18 '20 at 15:32
0

You can do allList = list1 + list2 + list3 instead of allList = [x for x in list1 + list2 + list3]

0

If you put the lists that you want to concatenate in a list literal, you can write a nested for loop in the list comprehension to loop over all elements.

allList = [n for l in [list1, list2, list3] for n in l]

This solution does not need any extra imports or other functions to concatenate the lists.

Rulle
  • 4,496
  • 1
  • 15
  • 21
0

As an alternative to all of the other options, you could also just splat them all into a new list for comprehension (if you don't explicitly require a comprehension to generate the concatenated list):

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> list3 = [9, 10, 11, 12]
>>> allList = [*list1, *list2, *list3]
>>> allList
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

I couldn't come up with any reason why the new list would need deriving from a comprehension explicitly since you could just nest this inside a comprehension if desired, e.g. if you had other logic to run on the merged list:

>>> [val + 3 for val in [*list1, *list2, *list3]]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

If performance is a big deal, you can also just make the splatted list a generator function instead:

>>> [val + 3 for val in (*list1, *list2, *list3)]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
bsplosion
  • 2,641
  • 27
  • 38
0

Simply do list1 + list2 + list3

>>> list1 = [1,2,3,4]
>>> list2 = [5,6,7,8]
>>> list3 = [9,10,11,12]
>>>
>>> list1 + list2 + list3
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

If you want to remove duplicates, run below code:

>>> list(set(list1 + list2 + list3))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Hedger
  • 112
  • 1
  • 6