Python | Swapping sublists over given range
Last Updated :
28 Jul, 2023
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 Python which can be performed for swapping sub lists over given range.
Swapping Sublists over Given Range using List Slicing
In Python, the sublists can be swapped using list slicing, as the lists can be swapped 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
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 , 6 , 7 , 11 , 14 , 15 , 2 ]
print ("The original list is : " + str (test_list))
test_list[ 1 : 3 ], test_list[ 6 : 8 ] = test_list[ 6 : 8 ], test_list[ 1 : 3 ]
print ("The list after sublist swapping : " + str (test_list))
|
Output
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]
Time Complexity: O(n)
Auxiliary Space: O(1)
Swapping Sublists over Given Range using slice()
In Python, the sublists can be swapped using slice(). 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
import itertools
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 , 6 , 7 , 11 , 14 , 15 , 2 ]
print ("The original list is : " + str (test_list))
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]))
print ("The list after sublist swapping : " + str (res))
|
Output
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]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the res list
Swapping Sublists over Given Range using numpy approach
In Python, the sublists can be swapped using Numpy. In this method, we will be using numpy arrays to perform the sublist swapping. Numpy’s advanced indexing will be used to swap the sublists.
Step-by-step approach:
- Convert the given list to a numpy array.
- Create two masks to get the two subarrays to be swapped.
- Swap the two subarrays using the masks.
- Convert the numpy array back to a list.
Python3
import numpy as np
test_list = [ 1 , 4 , 5 , 8 , 3 , 10 , 6 , 7 , 11 , 14 , 15 , 2 ]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
mask1 = np.zeros( len (arr), dtype = bool )
mask2 = np.zeros( len (arr), dtype = bool )
mask1[ 1 : 3 ] = True
mask2[ 6 : 8 ] = True
arr[mask1], arr[mask2] = arr[mask2], arr[mask1]
res = arr.tolist()
print ( "The list after sublist swapping : " + str (res))
|
Output
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]
Time Complexity: O(n), where n is the length of the list test_list.
Auxiliary Space: O(n), as we are creating two masks of size n.
Share your thoughts in the comments
Please Login to comment...