Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lambda expression which takes a tuple (a, b[], c) and return tuples flattening list b e.g. (a, b[0], c), (a, b[1], c)

Hi there I am trying to construct a lambda expression which takes in tuples of the form:
(a, b[], c). Where b is a list of elements which needs to be flattened.

for example the output of the expression should be:
(a, b[0], c), (a, b[1], c), (a, b[2], c) …

I’ve tried researching and looking for generator expressions examples for this context everywhere but cant really wrap my head around this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would appreciate it if someone could point me in the right direction.

>Solution :

You can use itertools.repeat() with zip() to create the desired tuple for each element in b:

from itertools import repeat

f = lambda a, b, c: zip(repeat(a), b, repeat(c))

print(*f(1, [2, 3, 4], 5))

This outputs:

(1, 2, 5) (1, 3, 5) (1, 4, 5)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading