How to remove duplicates from a list in python ?

The following solution is a quick method to remove duplicate elements from a list using python.

values = [3,4,1,2,2,4,4,4,4,4,6,2,1,3,2,4,5,1,4]
values = list(set(values))
print("Curated List--->", values)

The above snippet of code is self explanatory and hope this helps.

 

Python Tips & Tricks – 01

Problem Statement: You are calling a function that returns a list of values when you know that there’s only one result.  It will be a singleton list, but you don’t want to bother with that you just want the value of the first element.

Generic solution: put a [0] at the end of the expression:

values = getValues()[0]

Smart solution: Just put a comma (,) at the end of the variable:

values, = getValues()