I needed to remove the characters in string1
which are present in string2
. Here string1
and string2
have only the lower case characters a-z with given condition that the length of string1
will be greater every time.
I was using the in
operator:
def removeChars (string1, string2):
for char in string2:
if char in string1:
string1 = string1.replace(char, '')
return string1
But I read one answer on Stack Overflow which says:
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression
x in y
is equivalent toany(x is e or x == e for e in y)
.
Which means the in
operator is using a for
loop behind the scenes.
So my question is, in the for
loop in my code, should I consider that nested for
loops are being used, as the in
operator is using a for
loop in the background? If yes, what would be the time complexity of this program?