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.
# 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)) |
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.
# 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 = 1repl_list_end_idx = 4new_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)) |
The original list is : [4, 5, 6, 7, 10, 2] List after replacing sublist : [4, 11, 1, 10, 2]
Recommended Posts:
- Python | Shift sublist in list
- Python | Sort a list according to the second element in sublist
- Python | Find the sublist with maximum value in given nested list
- Python | Replace elements in second list with index of same element in first list
- Python | Replace substring in list of strings
- Python | Replace list elements with its ordinal number
- Python | Get last element of each sublist
- Python | Indexing a sublist
- Python | Maximum Sum Sublist
- Python | Get first element of each sublist
- Python | Getting sublist element till N
- Python | Split Sublist Strings
- Python | Find maximum value in each sublist
- Python | Convert nested sublist into tuples
- Python | Remove sublists that are present in another sublist
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.



