Open In App

Python – Remove index ranges from String

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string and ranges list, remove all the characters that occur in ranges. 

Input : test_str = ‘geeksforgeeks is best for geeks’, range_list = [(3, 6), (7, 10)] 
Output : geeks is best for geeks 
Explanation: The required ranges removed. 

Input : test_str = ‘geeksforgeeks is best for geeks’, range_list = [(3, 6)] 
Output : georgeeks is best for geeks 
Explanation : The required ranges removed.

Method #1: Using loop

In this, we check for each range, and remake string, considering the index doesn’t lie in range checking using conditional statements.

Python3




# Python3 code to demonstrate working of
# Remove index ranges from String
# Using loop
 
# initializing strings
test_str1 = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string 1 is : " + str(test_str1))
 
# initializing ranges list
range_list = [(3, 6), (7, 10), (14, 17)]
 
res = ""
 
for idx, chr in enumerate(test_str1):
  for strt_idx, end_idx in range_list:
       
    # checking for ranges and appending
    if strt_idx <= idx + 1 <= end_idx:
      break
  else:
    res += chr
 
# printing result
print("The reconstructed string : " + str(res))


Output

The original string 1 is : geeksforgeeks is best for geeks
The reconstructed string : geeksbest for geeks

Method #2 : Using any() + list comprehension + join()

In this, we perform the task of checking for indices for strings using any() and list comprehension is used to reconstruct string accordingly. 

Python3




# Python3 code to demonstrate working of
# Remove index ranges from String
# Using any() + list comprehension + join()
 
# initializing strings
test_str1 = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string 1 is : " + str(test_str1))
 
# initializing ranges list
range_list = [(3, 6), (7, 10), (14, 17)]
 
# using any() to check for strings in index ranges
res = ''.join(chr for idx, chr in enumerate(test_str1, 1) if not any(strt_idx <= idx <= end_idx
         for strt_idx, end_idx in range_list))
 
# printing result
print("The reconstructed string : " + str(res))


Output

The original string 1 is : geeksforgeeks is best for geeks
The reconstructed string : geeksbest for geeks

Time Complexity: O(n2 )
Auxiliary Space: O(n)

Method #3: Using itertools.compress()

  • Import the itertools module for compress operations.
  • Initialize an empty string res.
  • Initialize a list indices of the same length as test_str1, with all values set to True.
    Iterate over the range_list, and for each (start_idx, end_idx) in the list:
    • Update the corresponding indices in indices to False for the given range.
  • Use itertools.compress() function to filter out the characters from test_str1 using the indices list and join them to form the reconstructed string.
  • Print the reconstructed string res.

Python3




# Python3 code to demonstrate working of
# Remove index ranges from String
# Using itertools.compress()
 
import itertools
 
# initializing strings
test_str1 = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string 1 is : " + str(test_str1))
 
# initializing ranges list
range_list = [(3, 6), (7, 10), (14, 17)]
 
res = ""
 
indices = [True] * len(test_str1)
 
for start_idx, end_idx in range_list:
    for i in range(start_idx-1, end_idx):
        indices[i] = False
 
res = ''.join(itertools.compress(test_str1, indices))
 
# printing result
print("The reconstructed string : " + str(res))


Output

The original string 1 is : geeksforgeeks is best for geeks
The reconstructed string : geeksbest for geeks

Time complexity: O(n), where n is the length of the input string test_str1.
Auxiliary space: O(n), where n is the length of the input string test_str1.



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