Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to find the Index of an Element in a List in Python. so without wasting time lets learn about of this.
How to find the Index of an Element in a List in Python
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 the Index of an Element in a List in Python. Lets learn about of this by given below example:
lst = [12,14,16,18,20,101] print(lst.index(20))
Output :4
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 the Index of an Element in a List in Python. Lets learn about of this by given below example:
import numpy as np lst = [12,14,12,18,20,101,12] lst = np.array(lst) result = np.where(lst == 12) print(result[0])
Output :[0 2 6]
python list find index
To find the Index of an Element in a List in Python just Use numpy.where().By using numpy.where() you can find the Index of an Element in a List in Python. Lets learn about of this by given below example:
import numpy as np lst = [12,14,12,18,20,101,12] lst = np.array(lst) result = np.where(lst == 12) print(result[0])
Output :[0 2 6]
Method 1: Use index()
By using index() you can find the Index of an Element. Lets learn about of this by given below example:
lst = [12,14,16,18,20,101]
print(lst.index(20))
Output :
4
Method 2: Use numpy.where()
By using numpy.where() you can find the Index of an Element. Lets learn about of this by given below example:
import numpy as np
lst = [12,14,12,18,20,101,12]
lst = np.array(lst)
result = np.where(lst == 12)
print(result[0])
Output :
[0 2 6]
Method 3: Use type() operator
By using type() we can get the index of string elements. Lets learn about of this by given below example:
list1 = ['one',2,'three',4,'five',6]
for i in list1:
if(type(i) is str):
print(list1.index(i))
Output :
0
2
4
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read