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

Here, we will figure out how to discover and print the file of the first coordinated component of a list? To discover the list of a component, we use list.index(element) technique.

Given a list and we need to discover the record of first coordinated 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

list.index() Method:

It’s an inbuilt technique in Python, it restores the list of a first coordinated component of a list.

Syntax:

 list.index(element)

Here, the list is the name of the list and component is the component/thing whose originally coordinated file to be returned.

Program to discover the file of the first coordinated component in Python:

# 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

Leave a Comment

error: Alert: Content is protected!!