0

The exercise I'm doing requires me to create and print out a list containing all of the common elements in the 2 following lists without duplicates:

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'm trying to create the new list in one line of code and I think my logic is correct but obviously there's an issue with it somewhere.

Here's what is currently not working:

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]

common_list = []

common_list = [nums for nums in a if (nums in b and nums not in common_list)]

print(common_list)

I expect to get [1, 2, 3, 5, 8, 13] but the 1 is still duplicated even though I have the 'nums not in common_list' condition so I end up getting [1, 1, 2, 3, 5, 8, 13]

yatu
  • 86,083
  • 12
  • 84
  • 139
Phill
  • 3
  • 1
  • 2
    Have a look at python [sets](https://docs.python.org/2/library/sets.html) – yatu Jul 16 '19 at 14:31
  • 3
    `common_list` is only created after the _entire_ list comprehension has been claculated. – tobias_k Jul 16 '19 at 14:32
  • 4
    The value of `common_list` in your list comprehension is just `[]`. Only when the comprehension is complete will the new resulting list be assigned to `common_list`. – Tom Karzes Jul 16 '19 at 14:33
  • Possible duplicate of [Common elements between two lists with no duplicates](https://stackoverflow.com/questions/47454788/common-elements-between-two-lists-with-no-duplicates). Seems like this person had the same question as you. Another possible dupe: [Common elements comparison between 2 lists](https://stackoverflow.com/questions/2864842/common-elements-comparison-between-2-lists) – pault Jul 16 '19 at 14:43

5 Answers5

1

Instead of using a list I suggest you to use a set to avoid duplicate values.

common_set = set()

You can add items by:

common_set.add(value)

Finally you can print values by:

print(common_set)
Safak Ozdek
  • 906
  • 11
  • 18
1

As already mentioned in other answers and comment, your problem is that, during the list comprehension, common_list is empty.

Now for practical solutions: if order is not important, sets are your friends:

common_list = list(set(a) & set(b))

and if order is important, sets are still your friends:

seen = set()
bset = set(b) # makes `in` test much faster
common_list = []

for item in a:
    if item in seen:
        continue
    if item in bset:
        common_list.append(item)
        seen.add(item)
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0

you can use enumerate:

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]


res = [i for n, i in enumerate(a) if i not in a[:n] and i in b]
print (res)

output:

[1, 2, 3, 5, 8, 13]
ncica
  • 7,015
  • 1
  • 15
  • 37
0

One way to do this with lists is (assuming that the one of the lists has no duplicates):

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]

c = [x for x in a if x in b]

print(c)
# [1, 2, 3, 5, 8, 13]

or, for any list:


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]

c = []
for x in a + b:
    if x in a and x in b and x not in c:
        c.append(x)

print(c)
# [1, 2, 3, 5, 8, 13]

But sets are much better suited for this:

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}

c = a.intersection(b)
print(c)
# {1, 2, 3, 5, 8, 13}
StefOverflow
  • 139
  • 9
0

One-liner:

list(set(a).intersection(b))
Hyyudu
  • 134
  • 1
  • 7