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.……

1st Feb 2022, 3:27 AM
Lenoname
7 Réponses
+ 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 ])
1st Feb 2022, 4:01 AM
L.M.Paredes
L.M.Paredes - avatar
+ 5
print(Listx.index(3)) The index() method returns an integer that represents the index of first match of specified element in the List.
1st Feb 2022, 3:32 AM
L.M.Paredes
L.M.Paredes - avatar
+ 2
Just use index() method
1st Feb 2022, 3:36 AM
CLAD
CLAD - avatar
+ 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)
1st Feb 2022, 4:24 AM
L.M.Paredes
L.M.Paredes - avatar
1st Feb 2022, 5:15 AM
Mafdi
Mafdi - avatar
0
L.M.Paredes there are two threes in the list, it only prints the first one
1st Feb 2022, 3:57 AM
Lenoname
0
L.M.Paredes ”a for a” not just ”for a”? Whats len?
1st Feb 2022, 4:15 AM
Lenoname