Open In App

Python | Shift sublist in list

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with list, we can have a problem in which we need to shift some sublist to the desired index in the 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 a particular task. The pop function can be used to remove the sublist and insert function inserts the sublist. This happens for each element in a single iteration in a loop.

Python3




# Python3 code to demonstrate working of
# Shift sublist in list
# Using insert() + pop() + loop
 
# function to perform the task
 
 
def shift_sublist(test_list, strt_idx, no_ele, shft_idx):
    for ele in range(no_ele):
        test_list.insert(shft_idx + 1, test_list.pop(strt_idx))
    return test_list
 
 
# initializing list
test_list = [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Using insert() + pop() + loop
# Shift sublist in list
res = shift_sublist(test_list, 2, 3, 6)
 
# Printing result
print("The list after shift of sublist : " + str(res))


Output : 

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]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 

Method #2: Using list slicing 

This task can also be done using a list slicing technique in which one can just add the different sections of list at the required positions. 

Python3




# Python3 code to demonstrate working of
# Shift sublist in list
# Using list slicing
 
# function to perform the task
def shift_sublist(test_list, strt_idx, no_ele, shft_idx):
    return (test_list[:strt_idx] + test_list[strt_idx + no_ele : no_ele + shft_idx - 1]
            + test_list[strt_idx : strt_idx + no_ele] + test_list[shft_idx + no_ele -1:])
 
# initializing list
test_list = [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Using list slicing
# Shift sublist in list
res = shift_sublist(test_list, 2, 3, 6)
 
# Printing result
print("The list after shift of sublist : " + str(res))


Output : 

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]

Time Complexity: O(n) where n is the number of elements in the list “test_list”. Using list slicing performs n number of operations.
Auxiliary Space: O(n), where n is the length of the list.

Method #3: Using list comprehension and slicing

In this method, extract the sublist to be shifted using list slicing, create a new list by iterating through the original list and excluding the indices of the sublist, and insert the shifted sublist at the desired index using slicing.

Python3




def shift_sublist(test_list, strt_idx, no_ele, shft_idx):
    # Extract sublist to be shifted using list slicing and reverse it
    shifted_sublist = test_list[strt_idx:strt_idx+no_ele][::-1]
    # Create a new list by excluding indices of the sublist
    shifted_list = [test_list[i] for i in range(len(test_list)) if i not in range(strt_idx, strt_idx+no_ele)]
    # Insert the shifted sublist at the desired index using slicing
    shifted_list[shft_idx:no_ele+shft_idx] = shifted_sublist
    return shifted_list
 
# initializing list
test_list = [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16]
 
# printing original list
print("The original list is:", test_list)
 
# Using list comprehension and slicing
# Shift sublist in list
res = shift_sublist(test_list, 2, 3, 6)
 
# Printing result
print("The list after shift of sublist:", res)


Output

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, 12, 3, 7, 6]

Time complexity: O(n), where n is the length of the original list. This is because we only need to iterate through the original list once to extract the shifted sublist and create a new list.
Auxiliary space: O(n), as we create a new list to hold the shifted sublist and a new list to hold the remaining elements of the original list.

Method #4: Using reduce():

  1. Initialize the result as the original list.
  2. For each shift in the list of shifts:
    a. Call shift_sublist function on the result, passing in the current shift.
    b. Update the result with the returned value from shift_sublist.
  3. Return the final result.

Python3




from functools import reduce
 
def shift_sublist(test_list, strt_idx, no_ele, shft_idx):
    return (test_list[:strt_idx] + test_list[strt_idx + no_ele : no_ele + shft_idx - 1]
            + test_list[strt_idx : strt_idx + no_ele] + test_list[shft_idx + no_ele - 1:])
 
def shift_sublists(test_list, shifts):
    return reduce(lambda res, shift: shift_sublist(res, *shift), shifts, test_list)
 
test_list = [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16]
shifts = [(2, 3, 6), (5, 2, 9), (1, 4, 8)]
result = shift_sublists(test_list, shifts)
 
print("The original list is : " + str(test_list))
print("The list after shift of sublists : " + str(result))
#This code is contributed by Jyothi pinjala.


Output

The original list is : [4, 5, 6, 7, 3, 8, 10, 1, 12, 15, 16]
The list after shift of sublists : [4, 3, 12, 15, 6, 7, 16, 5, 8, 10, 1]

Time complexity: O(n), where n is the length of the input list. The shift_sublists() function applies shift_sublist() function to each tuple in shifts cumulatively using the reduce() function. Therefore, the time complexity of shift_sublists() function is O(mn), where m is the number of tuples in shifts.
Auxiliary space: O(n), where n is the length of the input list. The shift_sublists() function applies shift_sublist() function to each tuple in shifts cumulatively using the reduce() function. Therefore, the space complexity of shift_sublists() function is also O(n).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads