Most pythonic way of rotating a circular list to a canonical point

Emile van Sebille emile at
Sat Aug 1 22:49:58 CEST 2015

On 8/1/2015 1:34 PM, Lukas Barth wrote:
> Hi!
> I have a list of numbers that I treat as "circular", i.e. [1,2,3] and [2,3,1] should be the same. Now I want to rotate these to a well defined status, so that I can can compare them.
> If all elements are unique, the solution is easy: find the minimum element, find its index, then use mylist[:index] + mylist[index:], i.e. the minimum element will always be at the beginning.
> But say I have [0,1,0,2,0,3]. I can in fact guarantee that no *pair* will appear twice in that list, i.e. I could search for the minimum, if that is unique go on as above, otherwise find *all* positions of the minimum, see which is followed by the smallest element, and then rotate that position to the front.
> Now that seems an awful lot of code for a (seemingly?) simple problem. Is there a nice, pythonic way to do this?

Well, I have not understood the problem for such a seemingly simple one.  :)

Is the problem to determine if one list of circular numbers 'matches' 
another one despite rotation status?  If so, I'd do something like:

def matchcircularlists(L1,L2):
     #return True if L1 is a rotation variant of L2
     return "".join(map(str,L1)) in "".join(map(str,L2+L2))


More information about the Python-list mailing list