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
Flatten a List of Lists in Python
Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python. Using itertools.chain itertools modu
3 min read
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]
4 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
Iterate Over a List of Lists in Python
We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
2 min read
Add Elements of Two Lists in Python
Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Python - Convert a list into tuple of lists
When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists. For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this co
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 l
3 min read
Sort List of Lists Ascending and then Descending in Python
Sorting a list of lists in Python can be done in many ways, we will explore the methods to achieve the same in this article. Using sorted() with a keysorted() function is a flexible and efficient way to sort lists. It creates a new list while leaving the original unchanged. [GFGTABS] Python a = [[1,
3 min read
How to Find Index of Item in Python List
To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it
2 min read