0
How to know the position(index number) of a value in a list?
Listx = [5,8,3,4,3,6] Lets say i want the index position of the value 3. Positions start with 0 then 1.……
7 Antworten
+ 2
yes only the first if you want to find all matches you can use a for loop that gives you the index in a list
print([ a for a in range(len(Listx)) if Listx[a]==3 ])
+ 5
print(Listx.index(3))
The index() method returns an integer that represents the index of first match of specified element in the List.
+ 2
Just use index() method
+ 2
The len method gets the number of elements in list() so that the for loop returns from 0 to the number of elements in the list.
and the for the one that I put is more compact than
b=[]
for i in range(len(Listx)):
if (Listx[i]==3):
b.append(i)
print(b)
+ 2
I hope it helps
https://code.sololearn.com/cTKid3xA3IdP/?ref=app
0
L.M.Paredes there are two threes in the list, it only prints the first one
0
L.M.Paredes ”a for a” not just ”for a”? Whats len?