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
# Python3 code to demonstrate working of
# Adding list at beginning of list
# initialize list
test_list = [1, 4, 5, 7, 6]
# initialize add list
add_list = [3, 4, 2, 10]
# printing original list
print("The original list is : " + str(test_list))
# printing add list
print("The add list is : " + str(add_list))
# Adding list at beginning of list
test_list = add_list + test_list
# printing result
print("The original updated list is : " + str(test_list))
OutputThe 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
# Python3 code to demonstrate working of
# Adding list at beginning of list
# using deque.extendleft() + reversed()
from collections import deque
# initialize list
test_list = [1, 4, 5, 7, 6]
# initialize add list
add_list = [3, 4, 2, 10]
# printing original list
print("The original list is : " + str(test_list))
# printing add list
print("The add list is : " + str(add_list))
# Adding list at beginning of list
# using deque.extendleft() + reversed()
res = deque(test_list)
res.extendleft(reversed(add_list))
# printing result
print("The original updated list is : " + str(list(res)))
OutputThe 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
# Python3 code to demonstrate working of
# Adding list at beginning of list
# using "+" operator
# initialize list
test_list = [1, 4, 5, 7, 6]
# initialize add list
add_list = [3, 4, 2, 10]
# printing original list
print("The original list is : " + str(test_list))
# printing add list
print("The add list is : " + str(add_list))
add_list.extend(test_list)
test_list=add_list
# printing result
print("The original updated list is : " + str(test_list))
OutputThe 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
# Python3 code to demonstrate working of
# Adding list at beginning of list
# using "+" operator
# initialize list
test_list = [1, 4, 5, 7, 6]
# initialize add list
add_list = [3, 4, 2, 10]
# printing original list
print("The original list is : " + str(test_list))
# printing add list
print("The add list is : " + str(add_list))
for i in test_list:
add_list.append(i)
# printing result
print("The original updated list is : " + str(add_list))
OutputThe 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
# Python3 code to demonstrate working of
# Adding list at beginning of list
# using insert
# initialize list
test_list = [1, 4, 5, 7, 6]
# initialize add list
add_list = [3, 4, 2, 10]
# printing original list
print("The original list is : " + str(test_list))
# printing add list
print("The add list is : " + str(add_list))
for i,j in enumerate(test_list):
add_list.insert(i,j)
# printing result
print("The original updated list is : " + str(add_list))
OutputThe 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
Similar Reads
Python - Append items at beginning of dictionary The task of appending items at the beginning of a dictionary in Python involves inserting new key-value pairs at the start of an existing dictionary, rather than adding them at the end. Since dictionaries in Python preserve insertion order, this operation allows us to effectively reorder the diction
3 min read
Insert list in another list - Python We are given two lists, and our task is to insert the elements of the second list into the first list at a specific position. For example, given the lists a = [1, 2, 3, 4] and b = [5, 6], we want to insert list 'b' into 'a' at a certain position, resulting in the combined list a = [1, 2, 5, 6, 3, 4]
3 min read
Python Initialize List of Lists A list of lists in Python is often used to represent multidimensional data such as rows and columns of a matrix. Initializing a list of lists can be done in several ways each suited to specific requirements such as fixed-size lists or dynamic lists. Let's explore the most efficient and commonly used
3 min read
Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples:Add a Single Item Using append()append() method adds one item to the end of the li
3 min read
Get first and last elements of a list in Python The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way
3 min read
Appending To One List In A List Of Lists in a list in Python can be achieved using various methods, each with its own advantages. Choose the approach that best fits your specific requirements Lists of lists, also known as nested lists, are a powerful data structure in programming that allow for the organization of data in a hierarchical ma
4 min read
Insert after every Nth element in a list - Python Inserting an element after every Nth item in a list is a useful way to adjust the structure of a list. For Example we have a list li=[1,2,3,4,5,6,7]Let's suppose we want to insert element 'x' after every 2 elements in the list so the list will look like li=[1,2,'x',3,4,'x',5,6,7] Iteration with inde
4 min read
Python List Add/Append Programs This article covers a wide range of methods for adding elements to a list, including:Basic addition techniques like append(), extend(), and insert().Appending multiple items, lists, tuples, dictionaries and objects.Performing list modifications such as adding at the beginning, middle or end.Advanced
3 min read
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Appending a Dictionary to a List in Python Appending a dictionary allows us to expand a list by including a dictionary as a new element. For example, when building a collection of records or datasets, appending dictionaries to a list can help in managing data efficiently. Let's explore different ways in which we can append a dictionary to a
3 min read