Open In App

Python – Replace sublist from Initial element

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list and replacement sublist, perform the replacement of list sublist on basis of initial element of replacement sublist.

Input : test_list = [3, 7, 5, 3], repl_list = [7, 8] 
Output : [3, 7, 8, 3] 
Explanation : Replacement starts at 7 hence 7 and 8 are replaced. 

Input : test_list = [3, 7, 5, 3, 9, 10], repl_list = [5, 6, 7, 4] 
Output : [3, 7, 5, 6, 7, 4] 
Explanation : Replacement starts at 5 and goes till end of list.

Method 1: Using list comprehension + list slicing The combination of above functionalities can be used to solve this problem. In this, we extract the index of beginning element, and then perform the appropriate slicing of list according to requirements. 

Python3




# Python3 code to demonstrate working of
# Replace substring from Initial element
# Using list slicing + list comprehension
 
# initializing list
test_list = [5, 2, 6, 4, 7, 1, 3]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing repl_list
repl_list = [6, 10, 18]
 
# Replace substring from Initial element
# Extracting index
idx = test_list.index(repl_list[0])
 
# Slicing till index, and then adding rest of list
res = test_list[ :idx] + repl_list + test_list[idx + len(repl_list):]
 
# printing result
print("Substituted List : " + str(res))


Output : 

The original list : [5, 2, 6, 4, 7, 1, 3]
Substituted List : [5, 2, 6, 10, 18, 1, 3]

Time complexity: O(n), where n is the length of the original list.
Auxiliary space: O(n), as we create a new list to store the substituted list.

Method 2: Using for loop and conditional statements

This method uses a for loop to iterate over the elements in the original list, and conditional statements to decide whether to add the element to the final substituted list, or to replace it with the elements from the replacement list.

First, we find the index of the first element of the replacement list by iterating over the elements of the original list using a for loop and checking if each element is equal to the first element of the replacement list. Once we find the index, we break out of the loop.

Python3




test_list = [5, 2, 6, 4, 7, 1, 3]
repl_list = [6, 10, 18]
 
# find the index of the first element of the replacement list
for i in range(len(test_list)):
    if test_list[i] == repl_list[0]:
        idx = i
        break
 
# replace the substring from the initial element
res = []
for i in range(len(test_list)):
    if i < idx:
        res.append(test_list[i])
    elif i == idx:
        for j in range(len(repl_list)):
            res.append(repl_list[j])
    elif i > idx + len(repl_list) - 1:
        res.append(test_list[i])
 
print("Substituted List: ", res)


Output

Substituted List:  [5, 2, 6, 10, 18, 1, 3]

Time complexity: O(n), where n is the length of the original list.
Auxiliary space: O(n)

Method 3: Using extend method:

Algorithm:

1.Find the index of the first element in the replacement list in the original list.
2.Create a new list that consists of the elements before the index, the replacement list, and the elements after the index plus the length of the replacement list.
3.Print the resulting list.

Python3




test_list = [5, 2, 6, 4, 7, 1, 3]
repl_list = [6, 10, 18]
 
# Find the index of the first element in repl_list in test_list
idx = test_list.index(repl_list[0])
 
# Create a new list that consists of the elements before the index,
# the replacement list, and the elements after the index plus the length of the replacement list.
res = test_list[:idx]
res.extend(repl_list)
res.extend(test_list[idx+len(repl_list):])
 
# Print the resulting list
print("Substituted List:", res)
#This code is contributed  by Jyothi pinjala.


Output

Substituted List: [5, 2, 6, 10, 18, 1, 3]

Time complexity:

Finding the index of the first element in the replacement list in the original list takes O(n) time, where n is the length of the original list.
Slicing the original list to create the new list takes O(n) time, where n is the length of the original list.
Extending the new list with the replacement list and the remaining elements of the original list also takes O(n) time.
Printing the resulting list takes O(n) time.
Therefore, the overall time complexity of this code is O(n).
Space complexity:

The space complexity of the code is O(n + k), where n is the length of the original list and k is the length of the replacement list.
This is because we create a new list to hold the resulting list, which takes O(n + k) space, and we also use some constant space for storing variables and temporary values.

Method 4: Using the index() and insert() method

  1. Initialize the original list and the replacement list.
  2. Find the index of the first element of the replacement list in the original list.
  3. Use a for loop to insert the elements of the replacement list one by one into the original list, starting at the index found in step 2.
  4. Use a for loop to remove the elements of the original list that come after the index found in step 2 and before the end of the replacement list.
  5. Print the substituted list.

Python3




# Python3 code to demonstrate working of
# Replace substring from Initial element
# Using the index() and insert() method
 
# initializing list
test_list = [5, 2, 6, 4, 7, 1, 3]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing repl_list
repl_list = [6, 10, 18]
 
# Replace substring from Initial element
# Finding the index of the first element of the replacement list
idx = test_list.index(repl_list[0])
 
# Inserting the elements of the replacement list one by one into the original list
for i in range(len(repl_list)):
    test_list.insert(idx+i, repl_list[i])
 
# Removing the elements of the original list that come after the index found in step 2 and before the end of the replacement list
for i in range(len(repl_list)):
    test_list.pop(idx+len(repl_list))
 
# printing result
print("Substituted List : " + str(test_list))


Output

The original list : [5, 2, 6, 4, 7, 1, 3]
Substituted List : [5, 2, 6, 10, 18, 1, 3]

Time complexity: O(n^2) because of the two for loops.
Auxiliary space: O(n) because of the insertion and popping of elements from the original list.

Method 5: Using the slice() function and the list() constructor

Here’s another approach to replace a substring from the initial element using the slice() function and the list() constructor.

Steps:

  1. Initialize the original list and the list to replace with.
  2. Find the index of the first element of the list to replace with using the index() method.
  3. Create a new list by concatenating the slice of the original list from the start to the index of the first element to replace with, the list to replace with, and the slice of the original list from the index of the first element to replace with plus the length of the list to replace with to the end.
  4. Print the substituted list.

Python3




# Python3 code to demonstrate working of
# Replace substring from Initial element
# Using the slice() function and the list() constructor
 
# initializing list
test_list = [5, 2, 6, 4, 7, 1, 3]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing repl_list
repl_list = [6, 10, 18]
 
# Replace substring from Initial element
# Extracting index
idx = test_list.index(repl_list[0])
 
# Slicing and Concatenating
res = list(test_list[:idx]) + repl_list + list(test_list[idx + len(repl_list):])
 
# printing result
print("Substituted List : " + str(res))


Output

The original list : [5, 2, 6, 4, 7, 1, 3]
Substituted List : [5, 2, 6, 10, 18, 1, 3]

Time complexity: O(n), where n is the length of the original list.
Auxiliary space: O(n), where n is the length of the original list (to store the new list).



Last Updated : 09 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads