Practice Python

Beginner Python exercises

21 May 2014

List Remove Duplicates Solutions

Exercise 14

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.

Extras:

Sample solution

This solution has two different functions doing the solution in two ways - one does it with a loop, and one with sets!

# Exercise 13:
# Write a function that takes a list and returns a new list that contains
# all the elements of the first list minus duplicates.
# this one uses a for loop
def dedupe_v1(x):
y = []
for i in x:
if i not in y:
y.append(i)
return y
#this one uses sets
def dedupe_v2(x):
return list(set(x))
a = [1,2,3,4,3,2,1]
print a
print dedupe_v1(a)
print dedupe_v2(a)
view raw Ex13 hosted with ❤ by GitHub

Enjoying Practice Python?


Explore Yubico
Explore Yubico