-4

I'm trying to create a function to remove characters from one string if present in another.

for example:

l1 = ['a', 'b', 'c', 'd', 'e', 'f']

l2 = ['b', 'c', 'e']

I want it to return ['a', 'd', 'f']

I would like to have this within a function. Thank you for your help!

snovosel
  • 91
  • 1
  • 2
  • 10

3 Answers3

1

Here's a simple list comp approach:

def f(l1, l2):
    return [x for x in l1 if x not in l2]

l1 = ['a', 'b', 'c', 'd', 'e', 'f']
l2 = ['b', 'c', 'e']
print(f(l1, l2))
>>> ['a', 'd', 'f']

Here are a few more (using a filter you can say):

f = lambda l1, l2: list(filter(lambda elem: elem not in l2, l1))

If you want to modify the original list:

def f(l1, l2):
    for elem in l2:
        l1.remove(elem)
    return l1

l1 = ['a', 'b', 'c', 'd', 'e', 'f']
l2 = ['b', 'c', 'e']
print(l1) # Prints ['a', 'b', 'c', 'd', 'e', 'f']
print(f(l1, l2)) # Modifies l1 and returns it, printing ['a', 'd', 'f']
print(l1) # Prints ['a', 'd', 'f'] (notice the list has been modified)

If you need strings (and not lists as posted in your question), here's another lambda:

s1 = 'abcdef'
s2 = 'bce'
# Both of the below work with strings and lists alike (and return a string)
fn = lambda s1, s2: "".join(char for char in s1 if char not in s2)
# Or, using filter:
fn = lambda s1, s2: "".join(filter(lambda char: char not in s2, s1))

print(fn(s1, s2)
>>> 'adf'
Bahrom
  • 4,752
  • 32
  • 41
0

This should work

def subtract_list(list1, list2):
    return list(set(list1) - set(list2))
Querenker
  • 2,242
  • 1
  • 18
  • 29
  • FWIW, it's slightly more efficient (although more typing) to use the `set.difference` method rather than the `-` operator: eg, `''.join(set('abcdef').difference('bce'))`. – PM 2Ring Mar 26 '16 at 15:31
  • @PM2Ring Thanks for your suggestion. Can you give some explanation for this? As far as I know it is not clear whether he expect a list or a string as return value? – Querenker Mar 26 '16 at 16:04
  • No, it's not clear what the type of the input or output data is supposed to be, and since the OP didn't clarify that I voted to close the question as unclear. – PM 2Ring Mar 28 '16 at 06:57
  • 1
    The reason why `set.difference` is better than using `-` here is that it allows us to avoid creating a set from the second list (or string). Both operands of the `-` operator _need_ to be sets (or frozensets) to calculate the set difference. But the actual set difference algorithm doesn't really _need_ the 2nd operand to be a set, since it can simply iterate over the 2nd operand, so the `set.difference` method will accept any iterable as its argument. – PM 2Ring Mar 28 '16 at 06:58
  • (cont) If you want to see the details of how `set.difference` is implemented in standard Python (aka CPython), take a look at the [C source code](https://hg.python.org/cpython/file/a6e6efa2f4dd/Objects/setobject.c#l1589). It helps if you can read C, but it's not completely essential. :) – PM 2Ring Mar 28 '16 at 07:00
  • @PM2Ring Thanks, I found the function in CPython and now it is clear to me :) – Querenker Mar 28 '16 at 20:20
-1

You can use list comprehension

>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> l2 = ['b', 'c', 'e']
>>> l3 = [i for i in l1 if i not in l2]
>>> l3
['a', 'd', 'f'] 
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48