Search code examples
pythonlisttraversal

How to traverse a list starting from a selected point?


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.


Solution

  • 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]