Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the index of the second occurrence of a string inside a list

Tags:

python

string

This is my list and code:

x=[["hi hello"], ["this is other"],["this"],["something"],["this"],["last element"]]
for line in x:
    y=x.index(line)
    #code

The first time it gets "this", it works properly, but for the second time, it gets the index of the first "this" only!

How can I find the second occurrence of a string inside a list?

like image 833
no1 Avatar asked Dec 25 '22 21:12

no1


1 Answers

You could use enumerate(...) here.

>>> x=[["hi hello"], ["this is other"],["this"],["something"],["this"],["last element"]]
>>> for index, line in enumerate(x):
        print index, line


0 ['hi hello']
1 ['this is other']
2 ['this']
3 ['something']
4 ['this']
5 ['last element']
like image 158
Sukrit Kalra Avatar answered May 24 '23 08:05

Sukrit Kalra