-1

Say you have a list [1,2,3,4] And I want to get [2,3,4,1] or [3,4,1,2]. Basically I am using the list with a different starting point each time, but then continuing through the list. How would I create something to recognize that in python.

What i have now is list[n:] where n is the shifted value, say 2, making you start at three.

user2175034
  • 93
  • 1
  • 1
  • 3

2 Answers2

3
someList[n:] + someList[:n] 

would solve your purpose if n <= len(someList)

Also, collections.deque is the efficient way.

GodMan
  • 2,561
  • 2
  • 24
  • 40
  • Assuming `n < len(yourlist)`. Also, please [don't use `l` as a name here](http://www.python.org/dev/peps/pep-0008/#names-to-avoid) – askewchan Mar 15 '13 at 17:57
  • 1
    Thanks, +1. Also, you could just use `n %= len(someList)` first. – askewchan Mar 15 '13 at 18:01
  • Yes. Why not ! But, as it is your suggestion, please go ahead and edit the code with your contribution :) There is no reason that I should take credit for it – GodMan Mar 15 '13 at 18:05
2

I believe this is what you want

>>> def startAt(index, list):
...     print list[index:] + list[:index]
... 
>>> l = [0,1,2,3,4,5]
>>> startAt(3, l)
[3, 4, 5, 0, 1, 2]
>>>