5

i know that the index function work like this:

list = ['dog','cat','pizza','trump', 'computer', 'trump']
print list.index('trump')

the output will be 3. but now i want him for print the other 'trump' string, that come after 2 objects. but if i will do the same command:

print list.index('trump')

he will print 3 again - the first trump he see. so how do i move the 'offset' of the index function, so she will detect the other trump, in the index 5? thanks lot you guys!!

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Gerimeni
  • 83
  • 1
  • 5

6 Answers6

5

list.index takes a 2nd start argument:

>>> lst = ['dog','cat','pizza','trump', 'computer', 'trump']
>>> lst.index('trump') # 1st index
3
>>> lst.index('trump',4) # 2nd index
5
>>> lst.index('trump',lst.index('trump')+1) # 2nd index
5

Or if you want all indexes, use a list comprehension:

>>> [idx for idx,item in enumerate(lst) if item == 'trump']
[3, 5]
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
5

Just note the first index where the string appears, and pass the extra parameter to index the second time:

l = ['dog','cat','pizza','trump', 'computer', 'trump']
i = l.index("trump")
print(i)
print(l.index("trump",i+1))

I get:

3
5

from the inline doc, you can pass an optional start & stop value:

index(...)

L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.

(in a general case you have to protect your call to index by a try/except ValueError block in case the value is not in the list and act accordingly)

Note that start can be negative. If negative, the index is computed from the end of the list instead.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

If you want a list of all indices for an entry in your list, you can do something like this:

indices = [i for i, x in enumerate(list) if x == "key"]
1

create a shortened version of the list.

list=['dog','cat','pizza','trump', 'computer', 'trump']
print list.index('trump')
list = list[list.index('trump')+1:]
print list.index('trump')
E.D.
  • 639
  • 6
  • 14
1
l = ['dog','cat','pizza','trump', 'computer', 'trump']
i = [n for n, item in enumerate(l) if item == 'trump'] #find all indices

# print the result
for n in i:
   print n, l[n]

3 trump
5 trump
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
0

Try this...it work like charm.

Actually in your problem iteration executes only once even in for loop it wont work(not work for me even after iter and next() used)so I tried with while then I realize it is the game of iteration so put while in try with while 1: to start with iteration 1and index method does a linear search, and stops at the first matching item. If no matching item is found, it raises a ValueError exception.

CODE

list = ['dog','cat','pizza','trump', 'computer', 'trump']
value = "trump"
i = -1
try:
    while 1: # Start with Iteration 1 never 0
        i = list.index(value, i+1)
        print(value,"at", i)
except ValueError: # No matching item is found.
    pass

OUTPUT

trump at 3
trump at 5
Vrushal Raut
  • 1,050
  • 2
  • 15
  • 20
  • Found another easiest way to get common string in list #CODE `for idx,val in enumerate(l): if val=='trump': print(idx,val)` #OUTPUT `3 trump 5 trump` – Vrushal Raut May 24 '17 at 07:50