Unpacking lists in a f string
Python
python at example.invalid
Tue Feb 8 22:43:07 EST 2022
Paulo da Silva wrote:
> Hi!
>
> Let's say I have two lists of equal length but with a variable number of
> elements. For ex.:
>
> l1=['a','b','c']
> l2=['j','k','l']
>
> I want to build a string like this
> "foo a j, b k, c l bar"
>
> Is it possible to achieve this with f strings or any other
> simple/efficient way?
'foo ' + ', '.join(f"{one} {two}" for one,two in zip(l1,l2)) + ' bar'
More information about the Python-list
mailing list