In the previous article, we have discussed Python List extend() Method with Examples
List in Python:
Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.
List index() Method in Python:
The index() method returns the position in the given list of the given element.
Syntax:
gvnlist.index(element, start, end)
Parameter Values:Â
The list index() method can accept up to three arguments:
element – the to-be-searched element
start (optional) – Begin searching from this index
end (optional) – Search the element all the way up to this index
Return Value:
The index() method returns the index of the specified list element.
A ValueError exception is thrown if the element is not found.
Note:
Only the first occurrence of the matching element is returned by the index() method.
Examples:
Example1:
Input:
Given List = ['hello', 'this', 'is', 'python', 'programs'] Given Element = 'this'
Output:
The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index : 1
Example2:
Input:
Given List = ['hello', 'this', 'is', 'python', 'programs'] Given Element = 'btechgeeks'
Output:
Traceback (most recent call last): File "/home/d28185532d674af8eb476a05f17862ba.py", line 8, in <module> resltind = gvnlst.index(gvnele) ValueError: 'btechgeeks' is not in list
List index() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Example-1: If the element is present in the given list
Approach:
- Give the list as the static input and store it in a variable.
- Give the element whose index must be located in the given list and store it in another variable.
- Pass the given element to the index function for the given list and store the result in resltind variable.
- Print the resltind value.
- The Exit of the Program.
Below is the implementation:
# Give the list as the static input and store it in a variable. gvnlst = ['hello', 'this', 'is', 'python', 'programs'] # Give the element whose index must be located in the given list # and store it in another variable. gvnele = 'this' # Pass the given element to the index function for the given list # and store the result in resltind variable. resltind = gvnlst.index(gvnele) # Print the resltind value. print('The given element {', gvnele, '} in the given list', gvnlst, 'is present at the index :') print(resltind)
Output:
The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index : 1
Example-2: If the element is not present in the given list
Below is the implementation:
# Give the list as the static input and store it in a variable. gvnlst = ['hello', 'this', 'is', 'python', 'programs'] # Give the element whose index must be located in the given list # and store it in another variable. gvnele = 'btechgeeks' # Pass the given element to the index function for the given list # and store the result in resltind variable. resltind = gvnlst.index(gvnele) # Print the resltind value. print('The given element {', gvnele, '} in the given list', gvnlst, 'is present at the index :') print(resltind)
Output:
Traceback (most recent call last): File "/home/d28185532d674af8eb476a05f17862ba.py", line 8, in <module> resltind = gvnlst.index(gvnele) ValueError: 'btechgeeks' is not in list
Example-3: Giving Start Index
Below is the implementation:
# Give the list as the static input and store it in a variable. gvnlst = ['hello', 'this', 'is', 'python', 'programs'] # Give the element whose index must be located in the given list # and store it in another variable. gvnele = 'python' # Pass the given element to the index function for the given list # and store the result in resltind variable. # searching the element after 1 st index by passing second argument as 1 resltind = gvnlst.index(gvnele, 1) # Print the resltind value. print('The given element {', gvnele, '} in the given list', gvnlst, 'is present at the index :') print(resltind)
Output:
The given element { python } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index : 3
Example-4: Giving Start and End Index
Below is the implementation:
# Give the list as the static input and store it in a variable. gvnlst = ['hello', 'this', 'is', 'python', 'programs'] # Give the element whose index must be located in the given list # and store it in another variable. gvnele = 'python' # Pass the given element to the index function for the given list # and store the result in resltind variable. # searching the element after 0 st index to 2 index by passing # second argument as 0 and third argument as 2 resltind = gvnlst.index(gvnele, 0, 2) # Print the resltind value. print('The given element {', gvnele, '} in the given list', gvnlst, 'is present at the index :') print(resltind)
Output:
Traceback (most recent call last): File "/home/e3f1e4ebf5b32326c6525257b0b59752.py", line 10, in <module> resltind = gvnlst.index(gvnele, 0, 2) ValueError: 'python' is not in list
Explanation:
The element python is present at the third index but not between the indices, resulting in an error.
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the element whose index must be located in the given list as user input using the input() function and store it in another variable.
- Pass the given element to the index function for the given list and store the result in resltind variable.
- Print the resltind value.
- The Exit of the Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvnlst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the element whose index must be located in the given list # and store it in another variable. gvnele = int(input('Enter some random element = ')) # Pass the given element to the index function for the given list # and store the result in resltind variable. resltind = gvnlst.index(gvnele) # Print the resltind value. print('The given element {', gvnele, '} in the given list', gvnlst, 'is present at the index :') print(resltind)
Output:
Enter some random List Elements separated by spaces = 6 2 8 9 1 8 4 Enter some random element = 8 The given element { 8 } in the given list [6, 2, 8, 9, 1, 8, 4] is present at the index : 2
Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.