Filtering a List based on a Suffix and avoid duplicates I was wondering if there is a better way to solve this issue.
I have a list like this:
imp_list=[&quot;aa&quot;,&quot;daa&quot;,&quot;ab&quot;,&quot;as&quot;,&quot;aem&quot;,
      &quot;aum&quot;,&quot;aw&quot;,&quot;aa&quot;,&quot;acm&quot;,&quot;at&quot;,
      &quot;ar&quot;,&quot;aa_imp&quot;,&quot;daa_imp&quot;,&quot;ab_imp&quot;,
      &quot;as_imp&quot;]

I want to select all the strings that have the _imp suffix, plus all the the strings that have no _imp partner.
That's because aa_imp it's just a modified version of aa, and for me, in a large sense, it's a replica.
So I created this function:
def imputed_handler(my_list): 
  imp=[x for x in my_list if &quot;imp&quot; in x]

  cleaned_imp=set(map(lambda x: x.replace(&quot;_imp&quot;,&quot;&quot;),imp))

  not_imp=[x for x in my_list if &quot;imp&quot; not in x]
  set_list=set(my_list)
  no_replica=list(set_list-cleaned_imp)
  print(no_replica)
  return no_replica

Running the code as described above
test=imputed_handler(imp_list)

I get the following output:
['at', 'acm', 'aw', 'ar', 'daa_imp', 'aem', 'as_imp', 'ab_imp', 'aa_imp', 'aum']

Do better solutions exist? Thanks for your time, and let me know if something is not clear :)
First off, since you're looking for strings that end with _imp, x.endswith(&quot;_imp&quot;) is probably more reliable than &quot;imp&quot; in x.
Second, since we only want to remove _imp when it's a suffix, and since we know how long it is, we can use string slicing to remove the last 4 characters. That means we don't accidentally turn x_impabc_imp into xabc instead of x_impabc or something. Assuming that's not what we want to do. Maybe it is, maybe it isn't, maybe we'll never get an input like that so it won't matter either way, I have no idea.
I'm also thinking it might be nice to pass the suffix as a parameter. Maybe it might change at some point in the future. Maybe another part of the program uses a different suffix in a similar way. Doesn't hurt to be prepared.
In general, though, your approach is good. You find the _imp ones, you figure out what they are replacing, you remove the ones that have been replaced.
The neat thing is, you can do the first two steps all at once in a single pass through the input list. For example, you can do this:
def imputed_handler(my_list, suffix=&quot;_imp&quot;):
    my_set = set(my_list)
    cleaned_imp = set(item[:-len(suffix)] for item in my_set if item.endswith(suffix))

    return list(my_set - cleaned_imp)

Though a loop might be clearer at that point:
def imputed_handler(my_list, suffix=&quot;_imp&quot;):
    my_set = set(my_list)

    for item in my_list:
        if item.endswith(suffix):
            my_set.discard(item[:-len(suffix)])

    return list(my_set)