Search code examples
pythongenerator

creating an f-string generator expression and with index values


I don't have enough experience with generator expression to figure this out. I have a list and input statement like so:

work = ['Read', 'Write', 'Program', 'Email']
assign = input(f"Select what you have worked on > {(str(x) for x in work)}")

The above code is not working! The work list might change. I also want to see a different input message that is more like.

Select what you have worked on > 0: Read, 1: Write, 2: Program, 3: Email >

How do you recommend getting this input question?


Solution

  • Use str.join():

    >>> work = ['Read', 'Write', 'Program', 'Email']
    >>> f"Select what you have worked on > {', '.join(f'{i}: {x}' for i, x in enumerate(work))}"
    'Select what you have worked on > 0: Read, 1: Write, 2: Program, 3: Email'