35

How do i merge [['a','b','c'],['d','e','f']] to ['a','b','c','d','e','f']?

2
  • from functools import reduce a = [['a','b','c'],['d','e','f']] reduce(lambda x, y: x+y, a) Commented Feb 1, 2020 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)) Commented Aug 22, 2020 at 17:40

6 Answers 6

40

Using list comprehension:

ar = [['a','b','c'],['d','e','f']]
concat_list = [j for i in ar for j in i]
Sign up to request clarification or add additional context in comments.

6 Comments

This is the fastest way, fwiw. In terms of cpu cycles.
Totally the best way, as it's the most compact. But how does it work?
I love this! And I totally don't understand it! Please somebody explain. That is mindblowing.
This 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.
Excellent! Can anyone tell me how can we do a type conversion for the items before adding to concat_list?
|
21

list concatenation is just done with the + operator.

so

total = []
for i in [['a','b','c'],['d','e','f']]:
    total += i

print total

3 Comments

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.
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 Fully agree with you. total += i is a more generic solution.
9

This would do:

a = [['a','b','c'],['d','e','f']]
reduce(lambda x,y:x+y,a)

10 Comments

WHY?! this is completely over the top, why not just do a = [['a','b','c'],['d','e','f']] a[0]+a[1]
Will that work if the list was `[['a','b','c'],['d','e','f'],['d','e','f']] ?
I agree your answer is much more readable :), I just gave an generic answer. :)
@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...
@Sibi Why not simply use itertools.chain(), which is built for such for such purpose only and is very fast compared to your solution. stackoverflow.com/questions/406121/…
|
6

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']]))

1 Comment

sum() shouldn't be used for such things. As docs say: To concatenate a series of iterables, consider using itertools.chain().
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]

Comments

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']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.