How do i merge [['a','b','c'],['d','e','f']] to ['a','b','c','d','e','f']?
-
from functools import reduce a = [['a','b','c'],['d','e','f']] reduce(lambda x, y: x+y, a)Mohan– Mohan2020-02-01 16:51:19 +00:00Commented 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))javasundaram– javasundaram2020-08-22 17:40:15 +00:00Commented Aug 22, 2020 at 17:40
Add a comment
|
6 Answers
Using list comprehension:
ar = [['a','b','c'],['d','e','f']]
concat_list = [j for i in ar for j in i]
6 Comments
Jeff Snider
This is the fastest way, fwiw. In terms of cpu cycles.
Ferdinando Randisi
Totally the best way, as it's the most compact. But how does it work?
Alex
I love this! And I totally don't understand it! Please somebody explain. That is mindblowing.
GuillaumeDufay
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.Mainuddin
Excellent! Can anyone tell me how can we do a type conversion for the items before adding to concat_list?
|
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
Rushy Panchal
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.will
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.Sophia
@will Fully agree with you.
total += i is a more generic solution.This would do:
a = [['a','b','c'],['d','e','f']]
reduce(lambda x,y:x+y,a)
10 Comments
will
WHY?! this is completely over the top, why not just do
a = [['a','b','c'],['d','e','f']] a[0]+a[1]Sibi
Will that work if the list was `[['a','b','c'],['d','e','f'],['d','e','f']] ?
Sibi
I agree your answer is much more readable :), I just gave an generic answer. :)
will
@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...
Ashwini Chaudhary
@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/… |
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
Ashwini Chaudhary
sum() shouldn't be used for such things. As docs say: To concatenate a series of iterables, consider using itertools.chain().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]