Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Find List Element Smallest Index – index() Function with Examples

index() function is a part of python programming language and it is used to find the index of an element in list. Here element is the item of the list that index() method receives and try to find out its position index. Remember this position index starts from o, not from 1.

Let suppose there is a list with items (a, b, c, d) and we want to find the index of 'c'. So, how we find the index of value 'c'. The value 'c' will passed to the index() method and the method will return the index '2'.
If the the input value occurs more than one time (a, b, c, d, c) then index will show the position of very first element and ignore the others.

 

Syntax of index() Function

The syntax of index() function in Python is:

 

Parameters of index() Function in Python

x – Where x is any valid item passed as an argument. This is actually the value which we want to know the position index. This parameter is required.

 

Compatibility

index() function is available and compatible with both Python 2.x and 3.x. This function is one of python list methods.

 

Return Value of index() Function in Python

index() function will return a zero-based index of the given input in a list.

 

Python index() Function Example 1

There is a list of different fruits (apple, grapes, banana) and the goal is to find the index of a fruit grapes.

Output:
1
This is the index of color grapes.

 

Python index() Function Example 2

Now, there is a list of different colors (red, yellow, green, blue, orange, blue) and notice here blue color repeat twice.

Output:
3
Now, here is the index and this is the index of first blue color not second.

Loading...
LearnAndLearn.com © 2018 - All rights reserved