FAQ: Learn Python - Practice Makes Perfect - Remove_duplicates

This community-built FAQ covers the “Remove_duplicates” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Remove_duplicates_:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Alternate solution:-
vowels = [3,2,1,3,4,5,7]

vowels.sort()

print vowels

emp=[]

for i in vowels:
if i not in emp:
emp.append(i)

print emp

That works perfect, but “vowels” is a really bad variable name, why did you use it? :slight_smile:

I was practicing some using some other problems and rewrote my existing code to fit this .Yah variable name does not make sense. I agree.

Hey, I was wondering why this would not work for Remove_duplicates. Every time I press run, it loads for forever before determining that it didn’t work. I don’t need a piece of working code, I just want to know why this one doesn’t work.

def remove_duplicates(list_here):
dupe_removed_list = [ ]
for element1 in dupe_removed_list:
for element in list_here:
if element != element1:
dupe_removed_list.append(element)
return dupe_removed_list

I can’t seem to find any error codes that can help me find why this wouldn’t work if that helps.

There is no element in this list…

for element1 in dupe_removed_list:

so an empty list is returned.

1 Like

@mtf Thanks for the explanation Roy! One more question - would this code work if I put in a temporary element (like “test”) and removed said element before returning dupe_removed_list?

1 Like

That would be fudging. Better to not iterate that list in the outer loop. Try iterating in the inner loop when we know that element will point to a real value.

1 Like

Thanks for all the help @mtf

1 Like

def remove_duplicats(list):
res =
for i in list:
if i == i:
list.remove(i)
return list

print remove_duplicats([1,1,2,2])

it return the same result so why it’s wrong ???!!
you are making it difficult guys

Won’t that always be True?

I tried to write my code like this,
can you please please explain why it gives me error?
I don’t get the solution of the exercise “remove_duplicates” so I would like to find another easier solution…

def remove_duplicates(numbers):
numbers.sort()
emp =
for n in numbers:
if n[0] != n[1]:
emp.append(n)
print remove_duplicates([1, 1, 2, 3])

Sorting changes the order, which may not be such a good thing. n is an element value. Are we sure it is an iterable? We could go through the lines, but I think we need those questions answered first.

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

1 Like

That we have in-built constructors at our disposal, the goal here is not to exploit them. Instead, write an algorithm that will emulate the result. The above post does nothing to prove one’s own understanding.

My code is not working , pls help me out .

def remove_duplicates(list):
result=
newlist=list.sort()
print newlist
for x in newlist:
if x not in result:
result = result.append(x)
return result

print remove_duplicates([1, 1, 6,8,8,2, 2])

The return value of list.sort() is None. To assign a sorted list, we need to use the sorted() function.

newlist = sorted(list)

list.sort() is an in situ operation that takes place on the object.

list.sort()
newlist = list

However, now the original list is mutated, which may not be desirable. Hence, using the sorted function so the original keeps its order unchanged.

I saw the oficial answer and it shows “outputlist[-1]”. My question is what does the negative one means? We did not see on previous lessons.

Iterable objects can be traversed in both directions. From left to right, using positive indices, and from right to left, using negative indices. The index -1 refers to the first element from the right, as in the last element from the left.

Given an iterable, in this case a list, the complete range of indices is as follows:

 -7 -6 -5 -4 -3 -2 -1   # from right to left
  |  |  |  |  |  |  |
[ 1, 2, 3, 4, 5, 6, 7 ]
  |  |  |  |  |  |  |
  0  1  2  3  4  5  6   # from left to right
1 Like