Skip to content
Advertisement

Is there a way to make “print(l for l in List)” work?

Hi

I am trying to shorten my code with some for loops inside of print functions, but I run into this problem:

List = ["######################",
        "#                    #",
        "#                    #",
        "#     Hello There    #",
        "#                    #",
        "#                    #",
        "######################"]

print(l for l in List)

Output:

<generator object <genexpr> at 0x000001F704B0B820>

is there a way to make this work without doing:

for l in List:
    print(l)

Output:

######################
#                    #
#                    #
#     Hello There    #
#                    #
#                    #
######################

or do I have to stick with that?

I am sorry if this question is somewhere else, I just couldn’t find it. If you manage to find it, feel free to send a link instead of explaining it here too.

thanks in advance

Advertisement

Answer

You cannot use loop inside print() but you can do

spam = ["######################",
        "#                    #",
        "#                    #",
        "#     Hello There    #",
        "#                    #",
        "#                    #",
        "######################"]
print('n'.join(spam))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement