Retrieving the index of an element

The index() method returns the position of an element in a list:

    >>> index_list = [1, 2, 3, 4, 5, 6, 7]
>>> index_list.index(5)
4

In this example, the method returns the index of the element 5. Since Python uses zero-based indexing that is the index is counted from 0 and hence the index of the element 5 is 4:

    random_list = [2, 2, 4, 5, 5, 5, 6, 7, 7, 8]
>>> random_list.index(5)
3

In this example, the method returns the position of the first instance of the element. The element 5 is located at the third position.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset