Get unique values from a list in python
In Python, you can use the set() function to get the unique values from a list. Here is an example:
original_list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
unique_list = list(set(original_list))
print(unique_list)
This will output:
[1, 2, 3, 4, 5]
Watch a video course
Python - The Practical Guide
You can also use list comprehension and the in
operator.
original_list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
unique_list = [x for i, x in enumerate(original_list) if x not in original_list[:i]]
print(unique_list)
This will output the same result as above.