Insert list in another list - Python
Last Updated :
01 Feb, 2025
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].
Using list.insert()
insert() method allows us to insert an element at a specific index in the list. By looping through the elements of the second list, we can insert them one by one into the first list at the desired position.
Python
a = [1, 2, 3, 4]
b = [5, 6]
index = 2
# Loop through each element in b and insert it into a at the specified index
for item in b:
a.insert(index, item) # Insert item from b at the specified index
index += 1 # Increment index to add next item after the previous one
print(a)
Explanation:
- We use insert() to add each element from 'b' into 'a' at the specified index.
- After each insertion, we increment the index to ensure elements from 'b' are added sequentially.
Let's explore some more ways to insert on list in another list.
Using List Slicing
List Slicing method allows us to split the first list and insert the second list into the desired position.
Python
a = [1, 2, 3, 4]
b = [5, 6]
index = 2
# Slicing a before and after the index, then inserting b between
a = a[:index] + b + a[index:]
print(a)
Explanation:
- The list is sliced into two parts: before the specified index and after it.
- We concatenate 'b' between these two slices, effectively inserting it into 'a'.
Using extend()
extend() method can be used when we want to add all elements from the second list to the first list. This method is useful if we want to add the elements to the end or any specific index using slicing.
Python
a = [1, 2, 3, 4]
b = [5, 6]
index = 2
# Insert elements from b into a using slicing
a[index:index] = b
print(a)
Explanation:
- We use slicing to target the position where we want to insert 'b' and assign 'b' to that slice.
- This method allows us to insert 'b' without shifting individual elements.
itertools.chain() function can be used to combine multiple iterables into a single one. By using chain(), we can merge both lists efficiently.
Python
import itertools
a = [1, 2, 3, 4]
b = [5, 6]
index = 2
# Use itertools.chain() to combine the three parts of the list
a = list(itertools.chain(a[:index], b, a[index:]))
print(a)
Explanation: We use itertools.chain() to merge the parts of 'a' before and after the insertion index, along with the list 'b'.
Using append() and reverse()
This method first appends all elements from 'b' to the end of 'a' and then reverses the lists for proper positioning.
Python
a = [1, 2, 3, 4]
b = [5, 6]
index = 2
# Extend a with b, then reverse the remaining part of a after the insert index
a.extend(b)
a[index + len(b):] = reversed(a[index + len(b):]) # Reverse the rest of the list
# Insert b into a at the specified index
a[index:index] = b
print(a)
Output[1, 2, 5, 6, 3, 4, 6, 5]
Explanation: We first extend 'a' with 'b', then we reverse the remaining portion of the list to keep the original order intact after inserting 'b'.
Similar Reads
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
Move One List Element to Another List - Python The task of moving one list element to another in Python involves locating a specific element in the source list, removing it, and inserting it into the target list at a desired position. For example, if a = [4, 5, 6, 7, 3, 8] and b = [7, 6, 3, 8, 10, 12], moving 10 from b to index 4 in a results in
3 min read
Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul
3 min read
Python | Add list at beginning of list 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.
5 min read
List As Input in Python in Single Line Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com
3 min read