How to check if there is a repeated string in a list?

Asked

Viewed 70 times

0

I would like to learn an easier way to check all items on a list and search for repeated strings.

I’m currently doing it this way:

elif "azul" in fios[0] and fios[1] or "azul" in fios[1] and fios[2] or "azul" in fios[0] and fios[2]:
    print("Corte o último fio azul")

My intention is to check if there is more than one "blue" inside the yarn list, the code is working, but I’d like to make it more practical.

1 answer

0


Just use the method count of the object list:

cores = ['azul', 'amarelo', 'vermelho', 'preto']

print(cores.count('azul'))  # 1

cores = ['azul', 'amarelo', 'vermelho', 'azul']

print(cores.count('azul'))  # 2
  • Elif wires.Count("blue") is >= 2: turned right?

  • You don’t have the operator is, only the >=

Browser other questions tagged

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