Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift list elements to the right and shift list element at the end to the beginning

Tags:

python

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'], or [1,2,3] becomes [3,1,2].

I tried the following, but it's not working:

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])
like image 727
nesman Avatar asked Apr 07 '15 18:04

nesman


People also ask

How do you shift a list to a right in Python?

With Python, we can easily shift the items in a list both to the right or the left. To shift items to the left, we can remove the first element from the list with pop(), and then append it to the end of the list with the append() function. To shift items to the right, we can do the opposite.

How do you move the last element of a list in Python?

Method #2 : Using insert() + pop() This functionality can also be achieved using the inbuilt functions of python viz. insert() and pop() . The pop function returns the last element and that is inserted at front using the insert function.

How do I fetch the last element from a Python list with starting from the right side?

Here is the solution for your query. a=["first","second from last","last"] # A sample list print(a[0]) #prints the first item in the list because the index of the list always starts from 0. print(a[-1]) #prints the last item in the list. print(a[-2]) #prints the last second item in the list.

How do you move an array to the left in Python?

To shift the bits of array elements of a 2D array to the left, use the numpy. left_shift() method in Python Numpy. Bits are shifted to the left by appending x2 0s at the right of x1.


Video Answer


5 Answers

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

Usage:

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]
like image 198
Robᵩ Avatar answered Oct 24 '22 03:10

Robᵩ


You can use this:

li=li[-1:]+li[:-1]
like image 35
Srivatsa Sheshadri Avatar answered Oct 24 '22 05:10

Srivatsa Sheshadri


You can use negative indices together with list concatenation:

def shift(seq, n=0):
    a = n % len(seq)
    return seq[-a:] + seq[:-a]
like image 15
mkrieger1 Avatar answered Oct 24 '22 03:10

mkrieger1


If you are trying to shift the elements, use collections.deque rotate method:

#! /usr/bin/python3

from collections import deque
a = deque([1, 2, 3, 4])
a.rotate()
print(a)

Result:

[2, 3, 4, 1]
like image 10
aldeb Avatar answered Oct 24 '22 03:10

aldeb


You can just slice the last element off the list, then add it to the beginning of a new list:

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

Here is the result:

>>> aList = [1,2,3]
>>> aList = [aList[-1]] + aList[:-1]
>>> aList
[3, 1, 2]
like image 5
dotancohen Avatar answered Oct 24 '22 04:10

dotancohen