Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging sublists in python [duplicate]

Tags:

python

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'] 
like image 249
thclpr Avatar asked Jan 11 '13 12:01

thclpr


People also ask

Can you combine two lists in python?

In python, we can use the + operator to merge the contents of two lists into a new list. For example, We can use + operator to merge two lists i.e. It returned a new concatenated lists, which contains the contents of both list_1 and list_2.


2 Answers

Using list comprehension:

ar = [['a','b','c'],['d','e','f']] concat_list = [j for i in ar for j in i] 
like image 190
Ketouem Avatar answered Sep 21 '22 16:09

Ketouem


list concatenation is just done with the + operator.

so

total = [] for i in [['a','b','c'],['d','e','f']]:     total += i  print total 
like image 37
will Avatar answered Sep 21 '22 16:09

will