0

I have a list with elements I would like to remove from a string:

Example

list = ['345','DEF', 'QWERTY']
my_string = '12345XYZDEFABCQWERTY'

Is there a way to iterate list and find where are the elements in the string? My final objective is to remove those elements from the string (I don't know if is this the proper way, since strings are immutable)

2
  • look into regular expressions Commented Apr 11, 2017 at 20:40
  • Are you looking for find? Commented Apr 11, 2017 at 20:41

1 Answer 1

2

You could use a regex union :

import re

def delete_substrings_from_string(substrings, text):
    pattern = re.compile('|'.join(map(re.escape, substrings)))
    return re.sub(pattern, '', text)

print(delete_substrings_from_string(['345', 'DEF', 'QWERTY'], '12345XYZDEFABCQWERTY'))
# 12XYZABC
print(delete_substrings_from_string(['AA', 'ZZ'], 'ZAAZ'))
# ZZ

It uses re.escape to avoid interpreting the string content as a literal regex.

It uses only one pass so it should be reasonably fast and it ensures that the second example isn't converted to an empty string.

If you want a faster solution, you could build a Trie-based regex out of your substrings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.