Python | Program to Print the index of first matched element of a list

Here, we will learn how to find and print the index of first matched element of a list? To find the index of an element, we use list.index(element) method. Submitted by IncludeHelp, on July 25, 2018

Problem statement

Given a list and we have to find the index of first matched of a list in Python.

Example

Input:
List = [10, 20, 10, 20, 30, 40, 50]
element = 10
Output: 
Index of first matched 10 is: 0

Input:
List = [10, 20, 10, 20, 30, 40, 50]
element = 30
Output:
Index of first matched 20 is: 4

To get the index of first matched element of a list, you can use list.index() method by passing the element whose index to be found.

list.index() Method

It's an inbuilt method in Python, it returns the index of first matched element of a list.

Syntax:

 list.index(element)

Here, list is the name of the list and element is the element/item whose first matched index to be returned.

Python program to Print the index of first matched element of a list

# declare a list of Integers
list = [10, 20, 10, 20, 30, 40, 50]

# printing index of 10 
print (list.index (10))

#printing index of 20
print (list.index (20))

# printing index of 30
print (list.index (30))

# printing index of 40
print (list.index (40))

# printing index of 50
print (list.index (50))

Output

0
1
4
5
6

Python List Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.