Sometimes, while working with list, we can have a problem in which we need to shift some sublist to desired index in same sublist. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using
insert() + pop() + loop
The combination of above functions can be used to perform the particular task. The
pop function can be used to remove the sublist and
insert function inserts the sublist. This happens for each element in single iteration in loop.
The original list is : [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16]
The list after shift of sublist : [4, 5, 8, 10, 1, 6, 7, 3, 12, 15, 16]
Method #2 : Using list slicing
This task can also be using list slicing technique in which one can just add the different sections of list at required positions.
The original list is : [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16] The list after shift of sublist : [4, 5, 8, 10, 1, 6, 7, 3, 12, 15, 16]
- Python | Replace sublist with other 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 | Shift zeroes at end of list
- Python | Shift last element to first position in list
- Python | Maximum Sum Sublist
- Python | Indexing a sublist
- Python | Get first element of each sublist
- Python | Get last element of each sublist
- Python | Split Sublist Strings
- Python | Find maximum value in each sublist
- Python | Getting sublist element till N
- Python | Remove sublists that are present in another sublist
- Python | Convert nested sublist into tuples
- Python | Multiply each element in a sublist by its index
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.