Getting multiple index from a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

23rd Apr 2018, 5:53 AM
Andrews Essilfie
6 Answers
+ 9
https://code.sololearn.com/chDXJH9rUexT/?ref=app
23rd Apr 2018, 6:32 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 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()
23rd Apr 2018, 6:27 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 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.
23rd Apr 2018, 8:03 AM
Oma Falk
Oma Falk - avatar
23rd Apr 2018, 8:10 AM
Oma Falk
Oma Falk - avatar
+ 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
23rd Apr 2018, 6:27 AM
Johannes
Johannes - avatar
+ 2
You want to print the index, which is i.
23rd Apr 2018, 6:28 AM
Johannes
Johannes - avatar