I have only recently started learning Python, and have no experience coding. I'm trying to write code that, given a list of numbers, returns another (ordered) list of numbers without any of the duplicates present in the first list.
The following code seems to work:
nums = [2, 2, 1, 3]
r = []
for i in nums:
if i not in r:
r.append(i)
print(sorted(r))
Output is:
[1, 2, 3]
I then tried the same using list comprehension, but apparently got something wrong. This is what I tried:
nums = [2, 2, 1, 3]
r = []
r = sorted([i for i in nums if i not in r])
print(r)
And this is the output:
[1, 2, 2, 3]
What did I do wrong?