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
# 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]
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
# 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]
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
# Python3 code to demonstrate # swapping sublist using numpy approach import numpy as np # 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)) # convert the list to numpy array arr = np.array(test_list) # creating two masks to get the two subarrays to be swapped mask1 = np.zeros( len (arr), dtype = bool ) mask2 = np.zeros( len (arr), dtype = bool ) mask1[ 1 : 3 ] = True mask2[ 6 : 8 ] = True # swapping the two subarrays using the masks arr[mask1], arr[mask2] = arr[mask2], arr[mask1] # convert the numpy array back to a list res = arr.tolist() # printing result 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.