You can do it as:
a[a.index(8):]
a = [1,5,8,3,5,7]
>>> print a[a.index(8):]
[8,3,5,7]
Вопрос
How can I traverse a list with a starting point not equals to index 0?
In my specific case, I have have a list with numbers that may or may not be contiguous (for example: 1,2,5,8,11,35,664). And depending on user input, I want to start traversing from a certain point.
Using the same above example, suppose I want to start traversing from 8 onward.
Решение
You can do it as:
a[a.index(8):]
a = [1,5,8,3,5,7]
>>> print a[a.index(8):]
[8,3,5,7]
Другие советы
>>> for i in itertools.dropwhile(lambda x: x != 8, [1, 2, 5, 8, 11, 35, 664]):
... print i
...
8
11
35
664
>>> for i in itertools.islice([1, 2, 5, 8, 11, 35, 664], 3, None):
... print i
...
8
11
35
664
If your user's input is going to be the index value itself you can use:
for x in list[index:]
to simply go forward from from the index number.
If you want dictionary of {value:index}
. You can do like this. But it only work when element in the list are all unique.
>>> a = [1,2,5,8,11,35,664]
>>> b = dict(zip(a, xrange(len(a))))
>>> print "index of 8 is", b[8]
index of 8 is 3
>>> print "index of 35 is", b[35]
index of 35 is 5