225k views
3 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 MTarantini
by
4.8k points

1 Answer

6 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 Bgse
by
4.5k points