What would be the equivalent of find for lists in python?

3

What would be equivalent to find used in strings to use in lists? For example, if it is a=['a','b','c'] , how do I return to position 1 of 'b' ?

    
asked by anonymous 12.07.2018 / 13:23

1 answer

5

The find method, of string , returns the lowest index where the fetched value is found. In lists, there is the method index :

>>> numeros = [1, 2, 3, 4, 5]
>>> numeros.index(3)
2

That is, doing numeros.index(3) will return the index where the number 3 is - remembering that the index starts at 0, so the third value will be at index 2.

If the value is not found, a ValueError exception will be thrown.

>>> numeros = [1, 2, 3, 4, 5]
>>> numeros.index(9)
Traceback (most recent call last):
  File "python", line 1, in <module>
ValueError: 9 is not in list
    
12.07.2018 / 13:33