classification
Title: index function return first index for same element if repetitive in a list
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: chetanpalliwal13, rhettinger
Priority: normal Keywords:

Created on 2020-10-13 09:28 by chetanpalliwal13, last changed 2020-10-19 07:29 by rhettinger.

Files
File name Uploaded Description Edit
index-python-issue.JPG chetanpalliwal13, 2020-10-13 09:31
Messages (6)
msg378546 - (view) Author: Chetan Palliwal (chetanpalliwal13) Date: 2020-10-13 09:28
In [1]: po=[11,22,33,44,11,55,66,11,88]


In [2]: for lm in po:
   ...:     if lm==11:
   ...:         print("value is = {} and index is = {}".format(lm,po.index(lm)))
   ...:     else:
   ...:         print("value is = {} and index is = {}".format(lm,po.index(lm)))
   ...:
value is = 11 and index is = 0
value is = 22 and index is = 1
value is = 33 and index is = 2
value is = 44 and index is = 3
value is = 11 and index is = 0
value is = 55 and index is = 5
value is = 66 and index is = 6
value is = 11 and index is = 0
value is = 88 and index is = 8
msg378547 - (view) Author: Chetan Palliwal (chetanpalliwal13) Date: 2020-10-13 09:31
if an element in the list present more than 1 times in different order and we try to get the index of it python 3.6 is returning only the first index value for all places of that element in that list.
msg378551 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-10-13 10:00
The index() method is working as expected.  This isn't a bug.

The easiest way to accomplish your goal is to use enumerate() with a list comprehension:

    >>> po = [11,22,33,44,11,55,66,11,88]
    >>> [i for i, value in enumerate(po) if value == 11]
    [0, 4, 7]
msg378925 - (view) Author: Chetan Palliwal (chetanpalliwal13) Date: 2020-10-19 06:02
can you let it work using the index method then it index() is working fine. I can resolve it by using enumerate and another way.

The issue I raised here for the index() method not for enumerate.
msg378926 - (view) Author: Chetan Palliwal (chetanpalliwal13) Date: 2020-10-19 06:04
If the index() is working fine. why my previous attached code sample is returning 0 for each time I try to get the index for element 11 in the list.
msg378929 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-10-19 07:29
> If the index() is working fine. why my previous attached
> code sample is returning 0 for each time I try to get 
> the index for element 11 in the list.

Your code is buggy.  It assumes that list.index() changes its starting point as your for-loop iterates.   However, the documented and correct behavior of list.index() is that unless a *start* argument is provided, the search always starts at position zero and returns the index of the first matching target value:  

    >>> help(list.index)
    Help on method_descriptor:

    index(self, value, start=0, stop=9223372036854775807, /)
        Return first index of value.
History
Date User Action Args
2020-10-19 07:29:45rhettingersetmessages: + msg378929
2020-10-19 06:04:06chetanpalliwal13setmessages: + msg378926
2020-10-19 06:02:02chetanpalliwal13setstatus: closed -> open

messages: + msg378925
2020-10-13 10:00:41rhettingersetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-10-13 10:00:22rhettingersetnosy: + rhettinger
messages: + msg378551
2020-10-13 09:31:32chetanpalliwal13setfiles: + index-python-issue.JPG

messages: + msg378547
2020-10-13 09:28:18chetanpalliwal13create