Possible Duplicate:
Flattening a shallow list in Python
Making a flat list out of list of lists in Python
Merge two lists in python?
Fast and simple question:
How do i merge this.
[['a','b','c'],['d','e','f']]
to this:
['a','b','c','d','e','f']
Possible Duplicate:
Flattening a shallow list in Python
Making a flat list out of list of lists in Python
Merge two lists in python?
Fast and simple question:
How do i merge this.
[['a','b','c'],['d','e','f']]
to this:
['a','b','c','d','e','f']
Using list comprehension:
ar = [['a','b','c'],['d','e','f']]
concat_list = [j for i in ar for j in i]
list concatenation is just done with the + operator.
so
total = []
for i in [['a','b','c'],['d','e','f']]:
total += i
print total
This would do:
a = [['a','b','c'],['d','e','f']]
reduce(lambda x,y:x+y,a)
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']]))
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]
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']