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
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 L.pop(0) L.append(g) print(L) print("This is the new list")
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].
To implement this, first I made a user-defined list.
- 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.
- 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 )
- To input the elements in the list, I used a while loop which would append N number of elements in the list.
- One can enter the elements of their choice to the input method used with k as variable.
- Then the value entered in k is appended to the end of the list.
- The variable that holds the integral value for the number of elements is then decremented.
- Steps 4 to 6 are repeated until N>0.
- 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:
- Now we allocate the first element at the 0th index of list L to the variable g.
- Using the pop() method in Python, we remove the first element from the list L.
- Then, the append method appends the value in variable g to the list L.
- Hence all the elements are shifted to the left to the previous index.