Python - Replace sublist with others in list


In this article we will get to know how we can replace sublist value with new list. You may have faced this problem while working with manipulation of list. Here we will see various methods using which we can replace the sublist with other list.

Let’s get to know this using below example −

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]

Here we have list named as listItem which contains some elements and we have another list named as new_list. So, we want to replace the sublist of listItem with new_list. Take index range as 2 to 5 and if we replace these index values with new_list values then our listItem will be.

listItem : [1, 2, 7, 8, 9, 6]

Let’s see methods to perform this operation to replace the sublist with new_list.

Method 1. Using Split() and Join() Functions.

In this method we use slicing and concatenation of the list to replace a sublist with another new list.

Example

def replace_sublist(listItem, start, end, new_list):
   return listItem[:start] + new_list + listItem[end:]

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

Output

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

Explanation

Here in the above example we used slicing to get the position before and after the sublist. Then we concatenate the parts which are the part before the sublist, the new list and the part after the sublist. From the example you can observe we mentioned index range as 2 to 5 so from index and new list as [7,8,9] so our list will get updated at index [2..4] and our new list will be [1, 2, 7, 8, 9, 6].

Method 2. Using List Comprehension.

This method provides an easy way to traverse and create list using existing list.

Example

def replace_sublist(listItem, start, end, new_list):
   return [new_list[i - start] if start <= i < end else element for i, element in enumerate(listItem)]

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

Output

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

Explanation

Here in the above example we used list comprehension technique to traverse over the original list using enumerate() method. Then we replaced the listItem sublist element with the new list.

Method 3. Using List Assignment.

Example

def replace_sublist(listItem, start, end, new_list):
   listItem[start:end] = new_list
   return listItem

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

Output

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

Explanation

Here in the above example we used direct list assignment technique for directly assigning new list to the sublist position. We assigned new list to the sublist position using listItem[start : end] and python language will automatically replace sublist with new list. This is very easy approach compared to other methods.

Method 4. Using List Slicing and sum Operator.

Example

def replace_sublist(listItem, start, end, new_list):
   return listItem[:start] + new_list + listItem[end:]

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

Output

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

Explanation

Here in the above example we used list slicing method with sum (+) operator to replace the sublist. We extract the portion before and after the sublist and then we concatenate all those parts using the sum (+) operator.

Method 5. Using List Copy, Clear and Extend.

Example

def replace_sublist(listItem, start, end, new_list):
   copied_listItem = listItem.copy()
   del copied_listItem[start:end]
   copied_listItem.extend(new_list)
   return copied_listItem

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

Output

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

Explanation

Here in the above example we created a copy of the main list using list.copy() method, then we deleted the sublist of the given range from the copied list using del copied_listItem[start:end]. Then we extend the copied list with the new list elements using copied_listItem.extend(new_list). By performing these steps sublist will get replaced by the new_list items.

Method 6. Using List Indexing.

Example

def replace_sublist(listItem, start, end, new_list):
   for i in range(start, end):
      listItem[i] = new_list[i - start]
   return listItem

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

Output

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

Explanation

Here in the above example we used list indexing to replace the sublist items. We traverse over the range of indices of the sublist elements range(start, end) then we replace the sublist element with the elements of new_list using listItem[i] = new_list[i - start]. From the example you can observe from index 2 to 5 sublists will get replaced by new list items so, our list will get updated at index [2..4] and our new list will be [1, 2, 7, 8, 9, 6].

So, we get to know about various methods to replace the sublist using other new_list. We saw various approach to solve the problem which include list slicing, list comprehension, list indexing, split() method and many other ways can be there to replace these sublists. Each method has its unique approach to solve the problem, you can choose any method which is suitable and easy according to your need.

Updated on: 03-Oct-2023

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements