Hey I am having trouble trying to make a list of list into just one please help:
I want:
a = [['a'], 'b', 'c', 'd', 'e']
To become:
a = ['a', 'b', 'c', 'd', 'e']
Thanks
Hey I am having trouble trying to make a list of list into just one please help:
I want:
a = [['a'], 'b', 'c', 'd', 'e']
To become:
a = ['a', 'b', 'c', 'd', 'e']
Thanks
As long as your lists can't be nested more than one deep, you could do:
def flatten(lst):
for el in lst:
if isinstance(el, list):
yield from el
else:
yield el
Then call list
on the result if you actually need it as a list (usually an iterator will do).
a = [['a'], 'b', 'c', 'd', 'e']
flat_a = flatten(a) # not a list, but an iterator that returns flat values
flat_a_as_lst = list(flat_a) # actually a list
Try to iterate all sub-lists of the list. For your list a
:
a = [['a'], 'b', 'c', 'd', 'e']
flat_a = [subitem for item in a for subitem in (item if isinstance(item, list) else [item])]
Edit: considered Adams's comment.