+ 7

List index return not valid

When requesting index values from a list with identical items, the index value returned is not from the iteration, but from the first occurrence of list item. Is there a way to ascertain the index value of the repeated item? Code sample attached for clarification https://code.sololearn.com/cl13DK0N8pUi/?ref=app

30th Jan 2020, 12:59 AM
Rik Wittkopp
Rik Wittkopp - avatar
6 Antworten
+ 1
I don't think there is a way around it, maybe if you do it manually
30th Jan 2020, 2:24 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Hi ✳AsterisK✳ Jay Matthews has embedded a code in a reply to my sample code which shows a work around. Thanks for taking the time to look into it. 👍
30th Jan 2020, 2:30 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
I know this is a late reply but... List index method can take three params.....one of which is the starting index. myint = 4 mylist = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] print(mylist.index(myint)) print(mylist.index(myint, mylist.index(myint)+1)) <- return 8, index of second '4'. For more info:-https://docs.python.org/3/tutorial/datastructures.html
30th Jan 2020, 5:20 PM
rodwynnejones
rodwynnejones - avatar
+ 1
rodwynnejones Thanks. I will play with this also
30th Jan 2020, 6:06 PM
Rik Wittkopp
Rik Wittkopp - avatar
0
Thanks to Jay Matthews for his solution
30th Jan 2020, 2:19 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
Thanks Coder Kitten Another great idea for me to play with.
30th Jan 2020, 7:39 AM
Rik Wittkopp
Rik Wittkopp - avatar