-1

I'm trying to cycle from users' roles in my system.

I have a list from users' ids:

users = [12, 13, 14, 15]

I'd like to make a function when every time I call it I can change the order of my list:

#function()
users = [13, 14, 15, 12]
#function()
users = [14, 15, 12, 13]
#function()
users = [15, 12, 13, 14]

I looked into Google but I didin't find a good way to do that. I'm thinking in pop the first value and remake the list every time that my function is created and put the poped item in last position. Is there a more pythonic way to do it?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Jasar Orion
  • 626
  • 7
  • 26

2 Answers2

1

Try this:

def firsttolast():
    global users
    toappend = users.pop(0)
    users.append(toappend)

And you can use like this:

users = [12,13,14,15]
firsttolast()
print(users)

The whole code:

def firsttolast():
    global users
    toappend = users.pop(0)
    users.append(toappend)
users = [12,13,14,15]
firsttolast()
print(users)
ppwater
  • 2,315
  • 4
  • 15
  • 29
1

You can use slicing to make it circular. I wrote the example function but you can modify it according to your needs.

#CLOCKWISE
def changeSeq(x): return x[-1:]+x[:-1]
#ANTICLOCKWISE
def changeSeq(x): return x[1:]+x[0:1]
def changeSeq(x): return x[-1:]+x[:-1]

u = [12, 13, 14, 15]
print(u)
u = changeSeq(u)
print(u)
u = changeSeq(u)
print(u)

#OUTPUT
[12, 13, 14, 15]
[15, 12, 13, 14]
[14, 15, 12, 13]
crackaf
  • 492
  • 2
  • 11