0

How do I convert a list of lists to a single list?

Input:

a=[['AA'], ['AE'], ['AH'], ['AO'],]

Desired output:

['AA','AE','AH','AO']
jDo
  • 3,962
  • 1
  • 11
  • 30

3 Answers3

4
a=[['AA'], ['AE'], ['AH'], ['AO'],]

l=[]

for i in a:
      l.extend(i)

print l
jDo
  • 3,962
  • 1
  • 11
  • 30
Ola
  • 91
  • 1
  • 2
  • 8
0

you could use comprehension list:

or using map and lambda functions

a=[['AA'], ['AE'], ['AH'], ['AO'],]
# open the item [0] with generators
Newa = [x[0] for x in a]
>>>['AA', 'AE', 'AH', 'AO']

see examples: http://www.secnetix.de/olli/Python/list_comprehensions.hawk

Milor123
  • 537
  • 4
  • 20
-2

EDIT:

for i, value in enumerate(a):
    a[i] = value[0]
Cosinux
  • 321
  • 1
  • 4
  • 16