Python - print list elements that have 5 letters

0

Good, Having the following list:

lista_nomes =['Manuel', 'Laura', 'Antonio', 'Jasmim', 'Maria','Silvia', 'Lu', 'Pancrácio', 'Diogo', 'Ricardo', 'Miguel', 'Andre',]

I want to print the elements of the list that have 5 letters.

It turns out that I can not.

lista_nomes =['Manuel', 'Laura', 'Antonio', 'Jasmim', 'Maria',
'Silvia', 'Lu', 'Pancrácio', 'Diogo', 'Ricardo', 'Miguel', 'Andre',]


for elem in lista_nomes:
   if elem == 5:
       lista_valida.append(elem) 
print (elem)
    
asked by anonymous 01.05.2018 / 13:35

1 answer

4

You can use the len()

lista_nomes =['Manuel', 'Laura', 'Antonio', 'Jasmim', 'Maria', 'Silvia', 'Lu', 'Pancrácio', 'Diogo', 'Ricardo', 'Miguel', 'Andre',]
nomes = [nome for nome in lista_nomes if len(nome) == 5]
print(nomes)

See working on repl.it

    
01.05.2018 / 13:56