Python: moving items in a list to the right and moving an item at the end of the list to the beginning

I want to rotate items in a list, for example. - move the list items to the right so that ['a','b','c','d'] becomes ['d','a','b','c'] , or [1,2,3] became [3,1,2] .

I tried the following, but it does not work:

 def shift(aList): n = len(aList) for i in range(len(aList)): if aList[i] != aList[n-1]: aList[i] = aList[i+1] return aList elif aList[i] == aList[i-1]: aList[i] = aList[0] return aList shift(aList=[1,2,3]) 
+5
source share
9 answers

You can use negative indexes along with a list of concatenations:

 def shift(seq, n=0): a = n % len(seq) return seq[-a:] + seq[:-a] 
+5
source

If you are allergic to slice notation: a.insert(0,a.pop())

Using:

 In [15]: z=[1,2,3] In [16]: z.insert(0,z.pop()) In [17]: z Out[17]: [3, 1, 2] In [18]: z.insert(0,z.pop()) In [19]: z Out[19]: [2, 3, 1] 
+2
source

If you really want to move items, you can use modulo to cycle through the list and reassign items to their offset positions:

 def shift(lst, shft=0): ln = len(lst) for i, ele in enumerate(lst[:]): lst[(i + shft) % ln] = ele return lst In [3]: shift( ['a','b','c','d'] , 1) Out[3]: ['d', 'a', 'b', 'c'] In [4]: shift( ['a','b','c','d'] , 2) Out[4]: ['c', 'd', 'a', 'b'] In [5]: shift( ['a','b','c','d'] , 3) Out[5]: ['b', 'c', 'd', 'a'] 

If you only need one shift, just slide the last item forward, expanding the list:

 def shift(lst): lst[0:1] = [lst.pop(),lst[0]] return lst 

Both of them modify the original list.

+1
source

Simple use of slice syntax:

 def shift(seq): return [seq[-1]] + seq[:-1] assert shift([1, 2, 3, 4, 5]) == [5, 1, 2, 3, 4] 

Generalized version with interchangeable shifts:

 def shift(seq, shift=1): return seq[-shift:] + seq[:-shift] assert shift([1, 2, 3, 4, 5]) == [5, 1, 2, 3, 4] assert shift([1, 2, 3, 4, 5], 2) == [4, 5, 1, 2, 3] 
+1
source

The question seems to imply that the list itself needs to be modified, not the newly created list. Thus, a simple algorithm :

 lst = [1, 2, 3, 4, 5] e1 = lst[-1] for i, e2 in enumerate(lst): lst[i], e1 = e1, e2 print(lst) 

Donation:

 [5, 1, 2, 3, 4] 
+1
source

You can simply cut the last item from the list and then add it to the top of the new list:

 aList = [aList[-1]] + aList[:-1] 

Here is the result:

 >>> aList = [1,2,3] >>> aList = [aList[-1]] + aList[:-1] >>> aList [3, 1, 2] 
+1
source

If you are trying to move items, use collections.deque rotate :

 #! /usr/bin/python3 from collections import deque a = deque([1, 2, 3, 4]) a.rotate() print(a) 

Result:

 [2, 3, 4, 1] 
0
source

Using a function assuming n is a shift that is less than the length of the list l as follows:

 shift = lambda l, n: l[-n:] + l[:-n] # ie shift([1, 2, 3, 4], 3) 
0
source

This can be done simply using the list: insert method,

 values = [2, 3, 5, 7, 11, 13] def shift(list): new_list = [] for i in list: new_list.insert(len(new_list)-1, i) return new_list 

print (shift (values))

Exit:

 [3, 5, 7, 11, 13, 2] 
0
source

All Articles