I want to rotate elements in a list, e.g. - shift the list elements to the right so [\'a\',\'b\',\'c\',\'d\'] would become [\'d\',\'a\',\'b\',\'c\'], o
[\'a\',\'b\',\'c\',\'d\']
[\'d\',\'a\',\'b\',\'c\']
The question seems to imply to me that the list itself should be modified rather than a new list created. A simple in place algorithm is therefore:
lst = [1, 2, 3, 4, 5] e1 = lst[-1] for i, e2 in enumerate(lst): lst[i], e1 = e1, e2 print(lst)
Giving:
[5, 1, 2, 3, 4]