[How to check if my list has letters or words in Pyton]

Asked

Viewed 360 times

1

I have a list in Python and I need to know if it has any words or letters, because I need it to only have numbers

    def sum_of_products(lista):
if type(lista) != int:
    print("Possui letras")
else:
    print("Somente Numeros")

I thought of something like this, but I can’t find the solution.

  • And if you own the string '42', which is a string numerical, what must happen?

  • He would have to refuse and understand as if it were a word

1 answer

3


Assuming you have a list:

lista = [1,232,723,221,'numero',3455]

Simply put, you need to check by the list ITEMS, not by the list:

for item in lista:
    if type(item) != int:
        pass

Now checking if it exists more compact:

possui_letras = any(type(item) != int for item in lista)
if possui_letras:
    print("Possui letras")
else:
    print("Somente Numeros")
  • Exactly what I needed, it worked perfectly. Thank you very much!

Browser other questions tagged

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