Related Articles
Python | Replace sublist with other in list
• Last Updated : 28 Aug, 2019

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))`
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.

 `# 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))`
Output :
```The original list is : [4, 5, 6, 7, 10, 2]
List after replacing sublist : [4, 11, 1, 10, 2]
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up