How to break a line in the print of a python list?

Asked

Viewed 63 times

-2

My question is how to do the line break in a list print in Python. I need to print the code working. Except the names on the list are getting too long. Every 10, the winner gets 1 coupon, so if he donates 100 his name enters the list 10 times, making a huge line has how to do a line break in the print?

listasorteio = []

while True:
    print('digite sair para encerrar')
    nome = input('digite o nome: ')
    if nome == 'sair':
        break
    valordoado = int(input('Digite o valor doado: '))
    valord = valordoado // 2
    listasorteio.extend([nome]*valord)
print(listasorteio)

1 answer

-4


You can use List comprehension to call the function print on each element of the list:

[print(nome) for nome in listasorteio]

As the function print already contains the line break, must return the desired result.

For more information about list comprehension, here on the site exists enough material on the subject.

  • Hey buddy, it worked. Thanks.

  • @You’re welcome to Oldschoolgamer. If the answer suits you, don’t forget to accept it to help future site searches.

  • 1

    @Oldschoolgamer If you just wanted to print the names, one in each line, it’s much simpler to do for nome in listasorteio: print(nome). Use comprehensilist on in this case it is an exaggeration, not to mention that another list is created unnecessarily - the brackets around the expression create a list. If you just want to print and nothing else, why create this list, and you can print everything without creating it? See also: https://answall.com/a/459289/112052

  • right, got it and worked well too, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.