3

I'm new to python and I would like to get your advice regarding my function. What I want to do is below.

I have 2 lists A and B.(for example A = [1,2,3,4,5], B = [4,3,2,1]) I want to create a function which finds values in A which does not exist in list B. So in this case 5.

I wrote a function below but it does not work and I could not figure out what is wrong in the code.... Could anyone help me to understand what is the bug?? It seems easy but it is difficult for me. Thank you for your help!!

def finder(arr1,arr2):
    arr1 = sorted(arr1)
    arr2 = sorted(arr2)

    eliminated = []

    for x in arr1:
        if x not in arr2:
            eliminated = eliminated.append(x)
        else:
            pass
    return eliminated
yusuke0426
  • 273
  • 2
  • 4
  • 15
  • Change this `eliminated = eliminated.append(x)` to this `eliminated.append(x)` – idjaw Sep 19 '16 at 03:37
  • and also: http://stackoverflow.com/questions/642763/python-intersection-of-two-lists – fr_andres Sep 19 '16 at 03:38
  • `frozenset(a) - frozenset(b)` -- this will yield an immutable set that can be used as an iterable, it will have all items from a that are not in b. Note that the side effect will be that any duplicates in `a` or `b` will not be present nor will order be preserved. – Brian Cain Sep 19 '16 at 03:48
  • 1
    Set difference is the way to go for this problem, as it flexes it's O(n) muscles in the face of the `in` O(n^2) solution :D. – ospahiu Sep 19 '16 at 03:58

2 Answers2

3

The .append() method will modify the original list. Change the following line

eliminated = eliminated.append(x)

to

eliminated.append(x)

You also don't need to sort your lists.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Thank you! I changed my code as you mentioned. But the function still does not work yet. It does not return anything... Any thoughts? – yusuke0426 Sep 19 '16 at 04:00
  • 1
    @yusuke0426 You are most likely not printing the return of your method call. Simply calling your method will not output anything. You are *returning* your result, but are not actually printing anything. Just call a print around your method call – idjaw Sep 19 '16 at 04:04
  • @yusuke0426 Works fine for me: https://repl.it/Dc1p – Selcuk Sep 19 '16 at 04:43
  • @idjaw You were right! After I wrote print(finder(A,B)), it worked! But why simply calling the method do not print anything?? I thought the function itself returns values in eliminated list... – yusuke0426 Sep 19 '16 at 13:29
  • @yusuke0426 returning something from a method does not mean it will be printed. You have to explicitly print what you want showing in your program. – idjaw Sep 19 '16 at 13:32
  • @yusuke0426 The command line Python (REPL) will print the return value of a function, but when you run a `.py` file you have to explicitly us `print`. – Selcuk Sep 22 '16 at 03:56
3

Here are three different ways to do it. The first way uses set difference, the second uses the builtin filter feature, and the third uses a list comprehension.

Python 2.7.12 (default, Jul  9 2016, 12:50:33)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> A=[1,2,3,4,5]
>>> B=[4,3,2,1]
>>> def a(x,y):
...   return list(set(x)-set(y))
...
>>> a(A,B)
[5]
>>> def b(x,y):
...   return filter(lambda A: A not in y, x)
...
>>> b(A,B)
[5]
>>> def c(x,y):
...   return [_ for _ in x if _ not in y]
...
>>> c(A,B)
[5]
Charles D Pantoga
  • 4,307
  • 1
  • 15
  • 14