Search code examples
pythonlistdictionarylambda

How to unpack list created by map(lambda) inside a tuple?


I have a list of strings of which I want to have all the possible cased versions of a few string (e.g. 'yes' -> 'yes', 'Yes', 'YES', etc.). I'm using a map(lambda) construction, but I can't get the strings that are returned to be **not ** in a nested list like that. How do I do this?

>> (
some_other_objects,
*list((map(lambda s: list(map(''.join, itertools.product(*zip(s.upper(), s.lower())))), ['no', 'maybe'])))
)

# actual result:
(
some_other_objects,
['no', 'No', 'NO', 'nO', ...],
['Maybe', 'maybe', 'MAYBE', ...]
)

# desired result:
(
some_other_objects,
'no', 'No', 'NO', 'nO', ..., 
'Maybe', 'maybe', 'MAYBE', ...
)

Thank you in advance!! 🙂

Tried a lot with unpacking and listing already but nothing seems to work..


Solution

  • Unpack an itertools.chain:

    x = (
        'foo',
        *itertools.chain(*(map(lambda s: list(map(''.join, itertools.product(*zip(s.upper(), s.lower())))), ['no', 'maybe'])))
    )
    print(x)
    

    This outputs

    ('foo', 'NO', 'No', 'nO', 'no', 'MAYBE', 'MAYBe', 'MAYbE', 'MAYbe', 'MAyBE', 'MAyBe', 'MAybE', 'MAybe', 'MaYBE', 'MaYBe', 'MaYbE', 'MaYbe', 'MayBE', 'MayBe', 'MaybE', 'Maybe', ...)
    

    Further, I'd probably use a generator expression instead of map/lambda:

    import itertools
    
    x = (
        "foo",
        *itertools.chain(
            *(
                ("".join(c) for c in itertools.product(*zip(s.upper(), s.lower())))
                for s in ["no", "maybe"]
            )
        ),
    )
    
    print(x)