'how do I concatenate 3 lists using a list comprehension?
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#.
Solution 1:[1]
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))))
Solution 2:[2]
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]
Solution 3:[3]
You can do allList = list1 + list2 + list3 instead of allList = [x for x in list1 + list2 + list3]
Solution 4:[4]
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.
Solution 5:[5]
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]
Solution 6:[6]
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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | zs2020 |
| Solution 3 | Tiago Martins |
| Solution 4 | Rulle |
| Solution 5 | bsplosion |
| Solution 6 | Hedger |
