Python | Replace sublist with other in list

Sometimes, while working with Python, we can have a problem in which we need to manipulate a list in such a way that we need to replace a sublist with other. This kind of problem is common in web development domain. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop ( When sublist is given )
This method is employed in cases we know the sublist which is to replaced. We perform this task using loops and list slicing and divide the logic of finding the indices which needs to be manipulated first and then perform the replace.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Replace sublist with other in list
# Using loop (when sublist is given)
  
# helper function to find elements 
def find_sub_idx(test_list, repl_list, start = 0):
    length = len(repl_list)
    for idx in range(start, len(test_list)):
        if test_list[idx : idx + length] == repl_list:
            return idx, idx + length
  
# helper function to perform final task
def replace_sub(test_list, repl_list, new_list):
    length = len(new_list)
    idx = 0
    for start, end in iter(lambda: find_sub_idx(test_list, repl_list, idx), None):
        test_list[start : end] = new_list
        idx = start + length
  
# initializing list
test_list = [4, 5, 6, 7, 10, 2]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Replace list 
repl_list = [5, 6, 7]
new_list = [11, 1]
  
# Replace sublist with other in list
# Using loop (when sublist is given)
replace_sub(test_list, repl_list, new_list)
  
# printing result 
print("List after replacing sublist : " + str(test_list))

chevron_right


Output :



The original list is : [4, 5, 6, 7, 10, 2]
List after replacing sublist : [4, 11, 1, 10, 2]

 

Method #2 : Using list slicing ( When sublist index is given )
This task becomes easier when we just need to replace a sublist basic on the start and ending index available and list slicing is sufficient in such cases to achieve solution to this problem.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Replace sublist with other in list
# Using list slicing ( When sublist index is given )
  
# initializing list
test_list = [4, 5, 6, 7, 10, 2]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Replace list 
repl_list_strt_idx = 1
repl_list_end_idx = 4
new_list = [11, 1]
  
# Replace sublist with other in list
# Using list slicing ( When sublist index is given )
test_list[repl_list_strt_idx : repl_list_end_idx] = new_list
  
# printing result 
print("List after replacing sublist : " + str(test_list))

chevron_right


Output :

The original list is : [4, 5, 6, 7, 10, 2]
List after replacing sublist : [4, 11, 1, 10, 2]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.