[Solved] How to get rid of a list in Python when the element value of a list is a list [closed]


Try:

a = [('aa', 'bb', ['cc', 'dd']), ('ee', 'ff', ['gg', 'ff'])]

def flatten(tup):
    lst = []
    for x in tup:
        if isinstance(x, list):
            lst += x
        else:
            lst.append(x)
    return tuple(lst)

output = [flatten(t) for t in a]
print(output) # [('aa', 'bb', 'cc', 'dd'), ('ee', 'ff', 'gg', 'ff')]

As you pointed out, applying How to make a flat list out of a list of lists? would result in [('a', 'a', 'b', 'b', 'cc', 'dd'), ('e', 'e', 'f', 'f', 'gg', 'ff')] instead.

2

solved How to get rid of a list in Python when the element value of a list is a list [closed]