How do i merge [['a','b','c'],['d','e','f']]
to ['a','b','c','d','e','f']
?
Asked
Active
Viewed 4.6k times
34

Karl Knechtel
- 62,466
- 11
- 102
- 153

thclpr
- 5,778
- 10
- 54
- 87
-
from functools import reduce a = [['a','b','c'],['d','e','f']] reduce(lambda x, y: x+y, a) – Mohan Feb 01 '20 at 16:51
-
import itertools original_list = [[2,4,3],[1,5,6], [9], [7,9,0]] new_merged_list = list(itertools.chain(*original_list)) – javasundaram Aug 22 '20 at 17:40
6 Answers
40
Using list comprehension:
ar = [['a','b','c'],['d','e','f']]
concat_list = [j for i in ar for j in i]

Ketouem
- 3,820
- 1
- 19
- 29
-
-
3Totally the best way, as it's the most compact. But how does it work? – Ferdinando Randisi Apr 08 '16 at 13:38
-
I love this! And I totally don't understand it! Please somebody explain. That is mindblowing. – Alex Apr 19 '18 at 15:01
-
2This is double iteration in a list comprehension. The first `for` picks out each list in `ar` as `j`, and the second `for` picks out each element in `j` as `i`. – GuillaumeDufay Jul 06 '18 at 02:23
-
Excellent! Can anyone tell me how can we do a type conversion for the items before adding to concat_list? – Mainuddin Jun 05 '20 at 03:21
21
list concatenation is just done with the +
operator.
so
total = []
for i in [['a','b','c'],['d','e','f']]:
total += i
print total

will
- 10,260
- 6
- 46
- 69
-
This is the best solution, as it's the simplest. +1. However, you can simply do: `new_list = old_list1 + old_list2`. Your's does the same thing, but you don't need to put them together in a list of lists first. – Rushy Panchal Jan 11 '13 at 13:12
-
Yeah, I just wanted it to work for an arbitrary list. I've since learnt you can do something like `sum([], [list1, list2, list3])`, and since sum calls the + operator, which for `[]` is the concatenation op, it will join them all for you. – will Mar 14 '17 at 23:49
-
@will Fully agree with you. ```total += i``` is a more generic solution. – Sophia Oct 16 '22 at 18:29
8
This would do:
a = [['a','b','c'],['d','e','f']]
reduce(lambda x,y:x+y,a)

Sibi
- 47,472
- 16
- 95
- 163
-
WHY?! this is completely over the top, why not just do `a = [['a','b','c'],['d','e','f']]` `a[0]+a[1]` – will Jan 11 '13 at 13:00
-
Will that work if the list was `[['a','b','c'],['d','e','f'],['d','e','f']] ? – Sibi Jan 11 '13 at 13:01
-
obviously not, but that is not what the question was, and if you want it to work for arbitrarily long sets of flat lists, then [my answer](http://stackoverflow.com/a/14278710/432913) works and is much more readable – will Jan 11 '13 at 13:02
-
2I agree your answer is much more readable :), I just gave an generic answer. :) – Sibi Jan 11 '13 at 13:03
-
Apart from being slow(coz of `lambda`) this code will easily break if `a` is something like : `[('a','b','c'),['d','e','f']]`. – Ashwini Chaudhary Jan 11 '13 at 13:06
-
@Sibi - I normally dislike lambdas - i find they're a very easy way to make very unreadable code. This example i quite like for it's succinctness though – will Jan 11 '13 at 13:06
-
@AshwiniChaudhary Ofcourse, that code wasn't written to concatenate list and tuple ;) – Sibi Jan 11 '13 at 13:08
-
1@AshwiniChaudhary - I don't see how this is relevant - if you pass the wrong arguments to a function, you're going to get a bad answer... – will Jan 11 '13 at 13:09
-
@AshwiniChaudhary If you really want to make that code work, then you can do this for your case: `reduce(lambda x,y:list(x)+list(y),a)` – Sibi Jan 11 '13 at 13:10
-
2@Sibi Why not simply use `itertools.chain()`, which is built for such for such purpose only and is very fast compared to your solution. http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python#408281 – Ashwini Chaudhary Jan 11 '13 at 13:16
5
Try:
sum([['a','b','c'], ['d','e','f']], [])
Or longer but faster:
[i for l in [['a', 'b', 'c'], ['d', 'e', 'f']] for i in l]
Or use itertools.chain
as @AshwiniChaudhary suggested:
list(itertools.chain(*[['a', 'b', 'c'], ['d', 'e', 'f']]))

Hui Zheng
- 10,084
- 2
- 35
- 40
-
6`sum()` shouldn't be used for such things. As [docs](http://docs.python.org/2/library/functions.html#sum) say: **To concatenate a series of iterables, consider using itertools.chain().** – Ashwini Chaudhary Jan 11 '13 at 13:03
1
Try the "extend" method of a list object:
>>> res = []
>>> for list_to_extend in range(0, 10), range(10, 20):
res.extend(list_to_extend)
>>> res
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Or shorter:
>>> res = []
>>> map(res.extend, ([1, 2, 3], [4, 5, 6]))
>>> res
[1, 2, 3, 4, 5, 6]

Kiwisauce
- 1,229
- 8
- 7
0
mergedlist = list_letters[0] + list_letters[1]
This assumes you have a list of a static length and you always want to merge the first two
>>> list_letters=[['a','b'],['c','d']]
>>> list_letters[0]+list_letters[1]
['a', 'b', 'c', 'd']

Harpal
- 12,057
- 18
- 61
- 74