-3

I'm trying to get the indeces of the items of my list but on one item it returns a wrong index. There are two numbers who are the same in the list, maybe it confuses it with the first one.

The list is: [0,1,0,0,0,0,0,0,0,0,6,1,0]

for i in neu:

   if (i > 0):

      zahl = neu.index(i) + 7
      print(zahl)

      browser.find_element_by_xpath("//*[@id='tabelle_merkliste']/tbody/tr['zahl']/td[10]/img")

      print("found")

"print(Zahl)" returns these sums: 8 17 8 (should be 18) Maybe someone got an idea why this happens, thanks in advance.

Krischk
  • 35
  • 6
  • 1
    Please, edit your question to fix the indentation and post [mre] – buran May 26 '22 at 18:17
  • 1
    I guess, you expect 18, not 19 (second `1` is at index 11). However `list.index` will return the index of first occurrence, i.e. 1, not 11 – buran May 26 '22 at 18:20
  • 18 of course, changed it. I see.. you know a way to get the index of each item, ignoring if the item already appeared? – Krischk May 26 '22 at 18:24
  • use `enumerate()` – buran May 26 '22 at 18:25
  • Are the values in the list really '0', '1', ... (strings) or are they 0, 1, ... (numbers)? You should always post the actual code that you are running (and the posted code is not it). – jarmod May 26 '22 at 18:25
  • Does this answer your question? [Accessing the index in 'for' loops](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – mkrieger1 May 26 '22 at 18:28
  • Numbers, changed it sorry. – Krischk May 26 '22 at 18:28

1 Answers1

-1

Use enumerate:

for i, value in enumerate(neu):
    if (value > 0):

      zahl = i + 7
      print(zahl)

      browser.find_element_by_xpath("//*[@id='tabelle_merkliste']/tbody/tr['zahl']/td[10]/img")

      print("found")
H.K
  • 66
  • 5