Python | Swapping sublists over given range
The problem of swapping a single number can be extended to the issue of having the list and perform the swap over an entire range which can be a useful utility over a time. This has its application in any kind of data manipulation in various domains. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list slicing and swapping
The lists can be swapped the same ways a variable can be swapped in python but the difference is that instead of variable, we pass a sliced list to be swapped.
# Python3 code to demonstrate # swapping sublist # using list swapping and slicing # initializing list test_list = [1, 4, 5, 8, 3, 10, 6, 7, 11, 14, 15, 2] # printing the original list print ("The original list is : " + str(test_list)) # using list swapping and slicing # swapping sublist test_list[1 : 3], test_list[6 : 8] = test_list[6 : 8], test_list[1 : 3] # printing result print ("The list after sublist swapping : " + str(test_list)) |
The original list is : [1, 4, 5, 8, 3, 10, 6, 7, 11, 14, 15, 2] The list after sublist swapping : [1, 6, 7, 8, 3, 10, 4, 5, 11, 14, 15, 2]
Method #2 : Using slice() + itertools.chain.from_iterable()
The slice function can perform the slice functionality to extract a sublist from a list and from_iterable function helps to perform the swap functionality.
# Python3 code to demonstrate swapping # sublist using slice() + itertools.chain.from_iterable() import itertools # initializing list test_list = [1, 4, 5, 8, 3, 10, 6, 7, 11, 14, 15, 2] # printing the original list print ("The original list is : " + str(test_list)) # using slice() + itertools.chain.from_iterable() # swapping sublist slice_1 = test_list[1 : 3] slice_2 = test_list[6 : 8] slice_temp = [slice(0, 1), slice(6, 8), slice(3, 6), slice(1, 3), slice(8, len(test_list))] res = list(itertools.chain.from_iterable([test_list[i] for i in slice_temp])) # printing result print ("The list after sublist swapping : " + str(res)) |
The original list is : [1, 4, 5, 8, 3, 10, 6, 7, 11, 14, 15, 2] The list after sublist swapping : [1, 6, 7, 8, 3, 10, 4, 5, 11, 14, 15, 2]
Recommended Posts:
- Python | Remove all sublists outside the given range
- Python | Adding value to sublists
- Python | Merge corresponding sublists from two different lists
- Python | Print all sublists of a list
- Python | Add the occurrence of each number as sublists
- Python | Merge elements of sublists
- Python | Remove sublists that are present in another sublist
- Python | Print the common elements in all sublists
- Python | Sort all sublists in given list of strings
- Python | Count the sublists containing given element in a list
- Python | Split a list into sublists of given lengths
- Python | Remove repeated sublists from given list
- Python | Count unique sublists within list
- Python | Sort list of lists by the size of sublists
- Python | Split list of strings into sublists based on length
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.



