Python | Add list at beginning of list
Last Updated :
02 May, 2023
Sometimes, while working with Python list, we have a problem in which we need to add a complete list to another. The rear end addition to list has been discussed before. But sometimes, we need to perform an append at beginning of list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using “+” operator The “+” operator can be used to perform this particular task. In this, we just perform the addition of one list before other and construct a new list or perform the addition to same list.
Python3
test_list = [ 1 , 4 , 5 , 7 , 6 ]
add_list = [ 3 , 4 , 2 , 10 ]
print ( "The original list is : " + str (test_list))
print ( "The add list is : " + str (add_list))
test_list = add_list + test_list
print ( "The original updated list is : " + str (test_list))
|
Output
The original list is : [1, 4, 5, 7, 6]
The add list is : [3, 4, 2, 10]
The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the add_list
Method #2 : Using deque.extendleft() + reversed() This task can also be performed using combination of above methods. In this, we just convert the list into a dequeue, to allow a front append, and then one by one addition is done by extendleft(), the add list is reversed so that addition take place in correct order using reversed().
Python3
from collections import deque
test_list = [ 1 , 4 , 5 , 7 , 6 ]
add_list = [ 3 , 4 , 2 , 10 ]
print ( "The original list is : " + str (test_list))
print ( "The add list is : " + str (add_list))
res = deque(test_list)
res.extendleft( reversed (add_list))
print ( "The original updated list is : " + str ( list (res)))
|
Output
The original list is : [1, 4, 5, 7, 6]
The add list is : [3, 4, 2, 10]
The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using extend() method
Python3
test_list = [ 1 , 4 , 5 , 7 , 6 ]
add_list = [ 3 , 4 , 2 , 10 ]
print ( "The original list is : " + str (test_list))
print ( "The add list is : " + str (add_list))
add_list.extend(test_list)
test_list = add_list
print ( "The original updated list is : " + str (test_list))
|
Output
The original list is : [1, 4, 5, 7, 6]
The add list is : [3, 4, 2, 10]
The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Method #4 : Using append() method
Approach
- Initiate a for loop to traverse test_list
- Append each element to add_list
- Display add_list
Python3
test_list = [ 1 , 4 , 5 , 7 , 6 ]
add_list = [ 3 , 4 , 2 , 10 ]
print ( "The original list is : " + str (test_list))
print ( "The add list is : " + str (add_list))
for i in test_list:
add_list.append(i)
print ( "The original updated list is : " + str (add_list))
|
Output
The original list is : [1, 4, 5, 7, 6]
The add list is : [3, 4, 2, 10]
The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]
Method #5 : Using insert() method
Approach
- Initiate a for loop to traverse test_list
- Insert each element to add_list using index
- Display add_list
Python
test_list = [ 1 , 4 , 5 , 7 , 6 ]
add_list = [ 3 , 4 , 2 , 10 ]
print ( "The original list is : " + str (test_list))
print ( "The add list is : " + str (add_list))
for i,j in enumerate (test_list):
add_list.insert(i,j)
print ( "The original updated list is : " + str (add_list))
|
Output
The original list is : [1, 4, 5, 7, 6]
The add list is : [3, 4, 2, 10]
The original updated list is : [1, 4, 5, 7, 6, 3, 4, 2, 10]
Time Complexity: O(N) N length of the list
Auxiliary Space: O(M) M length of add_list
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...