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]