How do I return the position of an item in a list in Python?

Find the position of an element in a list using the index() method. In pythonIndex() method returns the position of an element in a list.

Note: Indexing starts with ‘0′ not ‘1’.

Let’s see an example of it:

list = [1,2,3,7,8,5]

print(list.index(3))

Output:

2

Let's see another example:

What happens in that case when we find out the position of an element that is not present in the given list.

list = [1,2,3,5,7]

print(list.index(4))

In that case, the compiler throws an error. i.e 4 is not in the list

What happens in that case, when the repeated element present in the list

Example:

list = [1,2,3,4,2,6,2]

print(list.index(2))

Output:

1

 


Comments

Popular posts from this blog

proassignment help backlink data index 41

Programming shark backlink data index 67

AHM Backlink Data Index 95