129,226 views
0 votes
0 votes
how do I write a python function that takes a list and returns a new list with unique elements from the first list?

User Venryx
by
2.3k points

1 Answer

18 votes
18 votes

Answer:

Step-by-step explanation:

The following Python program is a function that takes in a list as a parameter. It then uses the built-in dict class in Python to turn the input list into a dictionary and we call the fromkeys() method to remove all duplicate values. Then we turn the dictionary back into a list and save it into the variable called uniqueList. Finally, we return uniqueList back to the user. A test case has been created and the output can be seen in the attached image below.

def removeDuplicates(inputList):

uniqueList = list(dict.fromkeys(inputList))

return uniqueList

how do I write a python function that takes a list and returns a new list with unique-example-1
User Sir Athos
by
3.4k points