15.5k views
4 votes
Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates. The list: a = [3, 6, 9, 1, 3, 6, 6, 1, 1, 1]

User Grasaved
by
8.0k points

1 Answer

6 votes

Final answer:

To remove duplicates from a list in Python, use the set() function.

Step-by-step explanation:

To remove duplicates from a list in Python, you can use the set() function which automatically eliminates duplicate elements. Here's a program that takes a list and returns a new list without duplicates:

def remove_duplicates(lst):
return list(set(lst))

a = [3, 6, 9, 1, 3, 6, 6, 1, 1, 1]
new_list = remove_duplicates(a)
print(new_list)

This program defines a function called 'remove_duplicates' that takes a list as a parameter. It uses the set() function to convert the list into a set, which removes all duplicates. Finally, it converts the set back into a list using the 'list()' function and returns the new list.

User Marvin Ward Jr
by
9.3k points