Shifting Elements in a List to the Left in Python

In this tutorial, we will learn how to shift elements in a list to the left in Python with an example.

Lists are is ordered and changeable collection. It is very much like arrays, except they are one dimensional and in Python. lists in Python are written with square brackets. One can access the elements using indexes starting from 0 if traversal is from the front. If the traversal is from the rear end, then negative indexing can be done for traversal. For eg:= To access the last element one can use the index -1 and for second last element, -2 and so on.

To know the methods or functions related to list Datatype, one can type in

>>help(list)

 

in the Python Idle.

Shift elements in a list to the left in Python


def ShiftLeft():
        no=input("Enter the number of elements")
        N=int(no)
        L=[]
        while N>0:
             k=input("Enter an element")
             L.append(k)
        print(L)
        print("This is the original list")
        g=L[0]
        L.pop(0)
        L.append(g)
        print(L)
        print("This is the new list")

 

OUTPUT


Enter the number of elements 5
Enter an element 11
Enter an element 22
Enter an element 33
Enter an element 44
Enter an element 55
[11,22,33,44,55]
This is the original list
[22,33,44,55,11]
This is the new list

CODE EXPLANATION: Shifting list elements to the left


In the Code, Above we use a user-defined function “ShiftLeft” for the required program. The program emphasizes on Shifting the elements in a list to the Left. So, This means that all the elements are shifted to the previous index value and the first element now becomes the last element.

The index in the list is as -0, for 1st element;1, for 2nd element and -1 for last element of the list and so on…..

Suppose, we have a list allocated the variable A=[11,22,33,44,55,66]. So then after the program execution, A=[22,33,44,55,66,11].

List creation:

To implement this, first I made a user-defined list.

  1. I allocated a variable L in memory space as an empty list. Then I used a variable no to define the number of elements that I want in the list.
  2. Since input() always gives a string as a result, the variable no will have its type as str(string datatype). Thus to solve this problem, I typecasted it as an integer simply by allocating N variable a value of int(no). (This step would not be necessary in Python 2 )
  3. To input the elements in the list, I used a while loop which would append N number of elements in the list.
  4. One can enter the elements of their choice to the input method used with k  as variable.
  5. Then the value entered in k is appended to the end of the list.
  6. The variable that holds the integral value for the number of elements is then decremented.
  7. Steps 4 to 6 are repeated until N>0.
  8. Thus a list with no number of elements is created successfully. [Say N=5 and L=[11,22,33,44,55]]

Shifting elements to the Left:

  1. Now we allocate the first element at the 0th index of list L to the variable g.
  2. Using the pop() method in Python, we remove the first element from the list L.
  3. Then, the append method appends the value in variable g to the list L.
  4. Hence all the elements are shifted to the left to the previous index.

3 responses to “Shifting Elements in a List to the Left in Python”

  1. Patrick Gallagher says:

    This doesn’t work. The call to pop() takes off the last element of the list, meaning this code just replaces the last element in the list with the first element of the list without ever shifting the values.

  2. Ria says:

    Shifting occurs as the 2nd element (at index 1) becomes the first.

  3. Harshad Lukare says:

    Yes, Call to pop() method removes the last element of a list.
    Here in the code pop(0) method is used, which removes element at a specified index(0) from the list.
    Consider below updated code for the above topic :

    def ShiftLeft():
    no=input(“Enter the number of elements”)
    N=int(no)
    L=[]
    while N>0:
    k=input(“Enter an element”)
    L.append(k)
    N=N-1
    print(L)
    print(“This is the original list”)
    g=L[0]
    L.pop(0)
    L.append(g)
    print(L)
    print(“This is the new list”)

    ShiftLeft()

    Output:
    Enter the number of elements5
    Enter an element11
    Enter an element12
    Enter an element34
    Enter an element23
    Enter an element45
    [’11’, ’12’, ’34’, ’23’, ’45’]
    This is the original list
    [’12’, ’34’, ’23’, ’45’, ’11’]
    This is the new list

Leave a Reply

Your email address will not be published. Required fields are marked *