Home > Enterprise >  One-liner object comprehension in python3
One-liner object comprehension in python3

Time:12-15

names = ""
for p in portals: names = names   str(p)

Ie there a one-liner pythonic-way to achieve this ?

CodePudding user response:

Yes, you can use the str.join() method:

"".join(str(p) for p in portals)

Do note that there is even a more pythonic way to write

names = names   str(p)

and that way is

names  = str(p)

CodePudding user response:

The oneliner depends if the import counts:

from functools import reduce

reduce(lambda x, y: str(x)   str(y), portals)

Even if the list comprehension is a cleaner solution.

  • Related