+ 1
Getting multiple index from a list
When I run the code below instead of getting 1 and 2 as the index. I end up getting 1 repeated twice. How can I solve this. Thanks. https://code.sololearn.com/c6z4653dPHY1/?ref=app
6 odpowiedzi
+ 9
https://code.sololearn.com/chDXJH9rUexT/?ref=app
+ 8
.index() method find the *lowest* index number - or the *first* occurence of its argument in a list.
If youwant all of them, you can use enumerate()
+ 5
tlist = [2,5,5,6,7,2,4]
val = 5
i = 0
while i < len(tlist):
if tlist[i] == val:
print(tlist.index(val,i))
i = i+1
index has a start parameter.
just add it as i.
+ 2
tlist = [2,5,5,6,7,2,4]
val = 5
i = 0
while i < len(tlist):
if tlist[i] == val:
print(i)
i = i+1
+ 2
You want to print the index, which is i.