Question #6691   Submitted by Answiki on 09/21/2022 at 10:56:44 AM UTC

In Python, how to find the index of an element in a list?

Answer   Submitted by Answiki on 09/21/2022 at 10:58:14 AM UTC

In Python, the .index() method returns the index permet de retourner l'index of the first occurrence of a given element in a list. For example:

>>> myList =['zero', 'one', 'two', 'three', 'four', 'five']
>>> print (myList.index('three'))
3

In this example, it is the first occurrence that is returned:

>>> myList=["a","a","a","b","c","c"]
>>> myList.index('c')
4




2 events in history
Answer by Answiki on 09/21/2022 at 10:58:14 AM

In Python, the .index() method returns the index permet de retourner l'index of the first occurrence of a given element in a list. For example:

>>> myList =['zero', 'one', 'two', 'three', 'four', 'five']
>>> print (myList.index('three'))
3

In this example, it is the first occurrence that is returned:

>>> myList=["a","a","a","b","c","c"]
>>> myList.index('c')
4




Question by Answiki 09/21/2022 at 10:56:44 AM
In Python, how to find the index of an element in a list?

Icons proudly provided by Friconix.