3

Problem is this, take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

And write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

Here's my code:

a = [1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = []
for i in a:
    if i in b and i not in c:
        c.append([i])
print(c)

My output is still giving me duplicates despite the 'i not in c' statement. why is this? I'm sure its blatantly obvious, I just cant see it!

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Charlie Miller
  • 41
  • 1
  • 1
  • 4
  • why are you appending `[i]`? When `1 in c` looks in `c` and finds `[1]` it will not say they are the same. Remove the brackets. – Dimitris Fasarakis Hilliard Nov 23 '17 at 11:48
  • 3
    Possible duplicate of [Common elements comparison between 2 lists](https://stackoverflow.com/questions/2864842/common-elements-comparison-between-2-lists) – Ma0 Nov 23 '17 at 11:56

7 Answers7

14
  1. You are appending a list containing i to c, so i not in c will always return True. You should append i on its own: c.append(i)

Or

  1. Simply use sets (if order is not important):

    a = [1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    c = set(a) & set(b)  #  & calculates the intersection.
    print(c)
    #  {1, 2, 3, 5, 8, 13}
    

EDIT As @Ev. Kounis suggested in the comment, you will gain some speed by using
c = set(a).intersection(b).

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 2
    wouldn't `set(a).intersect(b)` be slightly faster since it avoids the casting to `set` of the second `list`? – Ma0 Nov 23 '17 at 11:57
  • 1
    @Ev.Kounis That's an interesting point, we will need to check the C code but I think that it's fair to assume that `.intersection` converts the argument to a set if it isn't, so it won't perform any better.. – DeepSpace Nov 23 '17 at 11:59
  • 1
    if I understand it correctly, the [bytecode](https://repl.it/repls/WickedAccurateAntbear) suggests it is a bit faster. It is a **`load global + binary and` vs `load attribute`** – Ma0 Nov 23 '17 at 12:03
  • 1
    @Ev.Kounis well, using `timeit.Timer` with `a = list(range(2000))` and `b = list(range(1500, 3000))` returned `0.023480591458422184` for `myfunc` and `0.012913368929072355` for `myfunc2`, so yes, it performs a tad faster. – DeepSpace Nov 23 '17 at 12:08
  • Actually this is more than a tad and certainly more than I thought. It is *almost twice* as fast. – Ma0 Nov 23 '17 at 12:16
  • thank you! knew it was something simple like that, i just couldnt see it for the life of me. – Charlie Miller Nov 23 '17 at 13:36
3

using list comprehensions, l think it had be short and simple if it was implemented as following

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[i for i in a and b if i in a and b]

result:

[1, 2, 3, 5, 8, 13]
Stefan
  • 1,697
  • 15
  • 31
Bee
  • 31
  • 4
0

The below code would work:

newlist = []
for x in b:
    if x in a:
        if x in newlist:
            print("duplicate")
        else:
            newlist.append(x)

for y in newlist:
    print(y)   
Enigma
  • 1,247
  • 3
  • 20
  • 51
0

Using intuition of sets, You could do something like this...

filtered_arr = list(set(b)-set(a))

First you convert 2 arrays into sets, then take the substitute of it convert the result into the list again.

Govinda Malavipathirana
  • 1,095
  • 2
  • 11
  • 29
0
c = []
for items in a:
    for numbers in b:
        if items == numbers:
            if items in c:
                None
            else:
                c.append(items)
print(c)
Adrian W
  • 4,563
  • 11
  • 38
  • 52
Deepu
  • 1
  • 1
  • 2
    While this code may provide a solution to problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution. – E. Zeytinci May 01 '20 at 11:58
0

Initialize list a, b and result

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

result = []

Append list result using list comprehension

[(result.append(i)) for i in a if i in b and i not in result]

print(result)   # [1, 2, 3, 5, 8, 13]
Raj Patel
  • 1
  • 1
  • Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – STA Mar 08 '21 at 09:18
0
a = set(a)
b = set(b)
c = a. intersection(b)
c = list(c)
print(c)
  • This is exactly what accepted answer suggests. – Martin Oct 10 '21 at 09:24
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 10 '21 at 11:21