+ 1

If numbers are repeated then how can we get index of other than 1st one

List index

4th May 2020, 9:28 AM
Amanillah Mansuri
Amanillah Mansuri - avatar
6 Answers
+ 5
Please mention the language name in tags where you have added lists
4th May 2020, 9:30 AM
Abhay
Abhay - avatar
+ 4
for i,j in enumerate(list) suppose there are similar values then you can check using i which is index for that value like If i==3: print(j) But if values are same and you want to get index You can try If j==5 print(i) I am sure it will print the first index for value 5 also you can do for index other than 1 If j==5 and i==3 then it will get the value 5 from 3rd index
4th May 2020, 9:35 AM
Abhay
Abhay - avatar
+ 4
Something like this if this is what you wanted to know a=[5,5,5,5,5] for i,j in enumerate(a): if i==3 and j==5: print(j)
4th May 2020, 9:40 AM
Abhay
Abhay - avatar
+ 3
Are you talking about the index method? somelist.index(whatever) This returns the first find. If you want to find another occurrence, you can define the start point of where you look. Let's say, you have found your first occurrence at index 5, then you can write: somelist.index(whatever, 6) Then, the method will return the first find *after* the first find. Using this, you can walk through the whole list in a while loop or something, and get all your finds.
4th May 2020, 9:44 AM
HonFu
HonFu - avatar
+ 3
[i for i in range(len(mylist) ) if mylist[i] == number] but enumerate is more pythonic.
4th May 2020, 10:16 AM
Oma Falk
Oma Falk - avatar
4th May 2020, 9:32 AM
Amanillah Mansuri
Amanillah Mansuri - avatar