Removing duplicate from list using list comprehension lead to all Nones

I tried the following code to remove duplicates from list using list comprehension

lists = [1, 2, 2, 3, 4, 3, 5, 6, 1]

unique_lists = []
[unique_lists.append(x) for x in lists if x not in unique_lists]
print(unique_lists)

unique_lists = []
g = [unique_lists.append(x) for x in lists if x not in unique_lists]
print(g)

The printed result are shown as below

[1, 2, 3, 4, 5, 6]
[None, None, None, None, None, None]

I’m trying to understand why the 2nd method (simply by assigning the list to g, returns all None? Thanks.

Answer

It’s because list.append() always returns None. The easy way to do what you want is to convert it to a set.

unique_lists = list(set(lists))