Python | Perform append at beginning of list
The usual append
operation of Python list adds the new element at the end of the list. But in certain situations, we need to append each element we add in front of list. If we perform brute force techniques, we need to perform unnecessary shifts of elements and hence, having shorthands for it is useful.
Let’s discuss certain ways to perform append at beginning of the list.
Method #1 : Using insert()
This method generally inserts the element at any position in the list and also performs the necessary shifts required internally and hence can also be used to perform this very task.
# Python3 code to demonstrate # to add element at beginning # using insert() # initializing list test_list = [ 1 , 3 , 4 , 5 , 7 ] # printing initial list print ( "Original list : " + str (test_list)) # using insert() to append # at beginning. append 6 test_list.insert( 0 , 6 ) # printing resultant list print ( "Resultant list is : " + str (test_list)) |
Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]
Method #2 : Using [] and +
These both operators can be combined to perform this task. We convert the element to list and then perform the list addition.
# Python3 code to demonstrate # to add element at beginning # using [] and + # initializing list test_list = [ 1 , 3 , 4 , 5 , 7 ] # printing initial list print ( "Original list : " + str (test_list)) # using [] and + to append # at beginning append 6 test_list = [ 6 ] + test_list # printing resultant list print ( "Resultant list is : " + str (test_list)) |
Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]
Method #3 : Using Slicing
Slicing of list is also another method to perform this particular task. we just assign to the 0 sliced list to the list converted from the element. This does the trick and is quite elegant.
# Python3 code to demonstrate # to add element at beginning # using slicing # initializing list test_list = [ 1 , 3 , 4 , 5 , 7 ] # printing initial list print ( "Original list : " + str (test_list)) # using slicing to append # at beginning. append 6 test_list[: 0 ] = [ 6 ] # printing resultant list print ( "Resultant list is : " + str (test_list)) |
Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]
Method #4 : Using collections.deque.appendleft()
The list can be converted to deque and then the appendleft()
can be used to perform the push like operation from the front of the doubly ended queue.
# Python3 code to demonstrate # to add element at beginning # using collections.deque.pushleft() from collections import deque # initializing list test_list = [ 1 , 3 , 4 , 5 , 7 ] # printing initial list print ( "Original list : " + str (test_list)) # using collections.deque.pushleft() # to append at beginning # append 6 test_list = deque(test_list) test_list.appendleft( 6 ) test_list = list (test_list) # printing resultant list print ( "Resultant list is : " + str (test_list)) |
Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]