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?
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With