1

This is a very simple question but I can't seem to understand why I am not getting it.

def listindex():
    li = ['a', 'e', 'a', 'd', 'b', 'a', 'e']
    for x in li:
        if x == 'a':
            print(li.index(x))

Result:

0
0
0

Expected Result:

0
2
5

Although it iterates over all the item I only get first item index, why is it? Also advise even though its pretty simple.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ilaunchpad
  • 1,283
  • 3
  • 16
  • 32
  • 3
    [RTFM](https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) - *"`s.index(x)` index of the **first occurrence** of x in s"* – jonrsharpe Oct 26 '15 at 15:40
  • 1
    @ilaunchpad Dont worry much about *internet points*. It is the learning which is important. Cheers. – Bhargav Rao Oct 26 '15 at 15:51
  • 2
    ...because it's trivially answered by reading the existing documentation, or [previous questions](http://stackoverflow.com/q/176918/3001761), or just `help(list.index)`? – jonrsharpe Oct 26 '15 at 15:51
  • 1
    @jonrsharpe I'm sorry not everyone understands everything from the documentation. If everyone only followed the documentation this place would be only solving questions for rocket scientist. – ilaunchpad Oct 26 '15 at 15:53
  • 1
    It's not like this is a complex edge case, or the documentation is even slightly unclear here. SO is not for every question, and we expect [a fair bit of research before asking](http://meta.stackoverflow.com/q/261592/3001761). Maybe you read the documentation and didn't understand it - that's OK, but then **mention that** and be specific about what's eluding you. You even acknowledge that it's not a good question **in the question** - *"This is a very simple question... Also advise even though its pretty simple."* Please take the [tour] and read [ask]. – jonrsharpe Oct 26 '15 at 15:55
  • 1
    @jonrsharpe I said it's simple because the logic is fairly simple even for me. And I explained which part I didn't get. I stated the items are iterated and I thought it should give me the index which seems pretty simple I don't seem to get. Anyways thanks!! – ilaunchpad Oct 26 '15 at 16:06

3 Answers3

6

index returns the index of the first element only. From the docs

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

Use enumerate instead. When you iterate using enumerate you can access the element and its index on the loop:

>>> li = ['a', 'e', 'a', 'd', 'b', 'a', 'e']
>>> for i,element in enumerate(li):
...     if element == 'a':
...        print(i)
... 
0
2
5
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
1
li = ['a', 'e', 'a', 'd', 'b', 'a', 'e']

Use List Comprehension:

[i for i, x in enumerate(li) if x=='a']

Output: [0, 2, 5]

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mushahid Khan
  • 2,816
  • 1
  • 19
  • 32
0

Use keyword enumerate() this will generate a counter from 0 to N.

for i, x in enumerate(li):

So i will contain the indexes for li.

Jackstine
  • 486
  • 4
  • 12