How to find the Index of an Element in a List in Python

Hi guys. How are yopu all? I hope you all are fine. Today in this tutorial we will learn about How to find the Index of an Element in a List in Python. we can find out the index number of an element in a list. There are some methods which will be useful to find it out. So here we will learn about that. So lets learn about to find the index of an element in a list in python. I hope this will help you. Lets learn it:

How to find the Index of an Element in a List in Python

  1. Find the Index of an Element in a List in Python

    to find the Index of an Element in a List in Python just Use index(). By using index() you can find out the index of an element in python. For this method first of all make your list After it use the index() function with the element you want to find out from the list. That will give the exact index of an emelemt in the list. You can see this in my output. So without wasting your time lets learn about that. I hope this will help you.
    mylst = [54,65,85,25,35,75] print(mylst.index(25)) Output : 3 If the element you want to find out from the list is not present in the liwst then it give you the valueError and giev you the sentence that the element is not in list. You can see this in my output. So lets learn this: mylst = [54,65,85,25,35,75] print (mylst.index(29)) Output : ValueError: 29 is not in list

  2. How to find the Index of an Element in a List in Python

    to find the Index of an Element in a List in Python just Use numpy.where(). By using numpy.where() you can find out the index of an element in list in python. Here this method will be useful to find the element when the element is present in the list more than one times. So you can find out the every position of the list in python. Lets learn this. import numpy as np mylst = [74,65,85,74,96,24,74] mylst = np.array(mylst) result = np.where(mylst == 74) print(result[0]) Output : [0 3 6]

Method 1: Use index()

By using index() you can find out the index of an element in python. For this method first of all make your list After it use the index() function with the element you want to find out from the list. That will give the exact index of an emelemt in the list. You can see this in my output. So without wasting your time lets learn about that. I hope this will help you.

mylst = [54,65,85,25,35,75]
print(mylst.index(25))

Output :

3

If the element you want to find out from the list is not present in the liwst then it give you the valueError and giev you the sentence that the element is not in list. You can see this in my output. So lets learn this:

mylst = [54,65,85,25,35,75]
print (mylst.index(29))

Output :

ValueError: 29 is not in list

Method 2: Use numpy.where()

By using numpy.where() you can find out the index of an element in list in python. Here this method will be useful to find the element when the element is present in the list more than one times. So you can find out the every position of the list in python. Lets learn this.

import numpy as np
mylst = [74,65,85,74,96,24,74]
mylst = np.array(mylst)
result = np.where(mylst == 74)
print(result[0])

Output :

[0 3 6]

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment