B.Kocis
B.Kocis

Reputation: 2020

how to unpack a nested list in Python

How to unpack a list, which is nested inside another list. Practically to transform this:

l=['aa','bb',['ccc','ddd'],'ee'] 

to

l=['aa','bb','ccc','ddd','ee']

Upvotes: 0

Views: 366

Answers (1)

embert
embert

Reputation: 7592

See this thread and e. g. the solution of elqott

>>> from compiler.ast import flatten
>>> l = ['1','2',['3','4'],'5'] 
>>> flatten(l)

Following your edit

['1', '2', '3', '4', '5']
>>> l = ['aa','bb',['ccc','ddd'],'ee'] 
>>> flatten(l)
['aa', 'bb', 'ccc', 'ddd', 'ee']

Upvotes: 1

Related Questions