Given a list and replacement sublist, perform the replacement of list sublist on basis of initial element of replacement sublist.
Input : test_list = [3, 7, 5, 3], repl_list = [7, 8]
Output : [3, 7, 8, 3]
Explanation : Replacement starts at 7 hence 7 and 8 are replaced.
Input : test_list = [3, 7, 5, 3, 9, 10], repl_list = [5, 6, 7, 4]
Output : [3, 7, 5, 6, 7, 4]
Explanation : Replacement starts at 5 and goes till end of list.
Method : Using list comprehension + list slicing
The combination of above functionalities can be used to solve this problem. In this, we extract the index of beginning element, and then perform the appropriate slicing of list according to requirements.
The original list : [5, 2, 6, 4, 7, 1, 3] Substituted List : [5, 2, 6, 10, 18, 1, 3]
- Python | Replace sublist with other in list
- Python | Sort a list according to the second element in sublist
- Python | Getting sublist element till N
- Python | Get last element of each sublist
- Python | Get first element of each sublist
- Python | Multiply each element in a sublist by its index
- Python - Join Tuples if similar initial element
- replace() in Python to replace a substring
- Python | Pandas Series.str.replace() to replace text in a series
- Python | Shift sublist in list
- Python | Find maximum value in each sublist
- Python | Maximum Sum Sublist
- Python | Indexing a sublist
- Python | Remove sublists that are present in another sublist
- Python | Find the sublist with maximum value in given nested list
- Python | Split Sublist Strings
- Python | Convert nested sublist into tuples
- Python - Incremental Sublist Sum
- Python | Mean of consecutive Sublist
- Python - Sublist Maximum in custom sliced List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.