I'm trying to solve this problem using list comprehensions. I want to take the list:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and create a new list b
containing all the numbers from a
that are below 5, excluding repeats.
I can do this using a for loop:
b = list()
for i in a:
if i < 5 and i not in b:
b.append(i)
which gives me [1, 2, 3]
, but when I try this using a list comprehension:
b = list()
b = [i for i in a if i not in b and i < 5]
I get a list with repeated values: [1, 1, 2, 3]
Is there a way to exclude repeated values when using list comprehensions?