How can I use “not in” to remove duplicates from a list?

Question

How can I use “not in” to remove duplicates from a list?

Answer

When we use not in, it results in True only if the object on the left is not present in the object on the right. For example, if we have a list of numbers and want to know if the number 10 is present in it, and print a message to the user if it is not, we could write:

if 10 not in my_list:
  print "10 not found!"

To expand on that, we can check for duplicates by creating an empty list to put our non-duplicate numbers into. Then, inside of a loop that goes through our input list, we’d do a check similar to the code above and look for the current loop variable value to not be in our output list. If it isn’t, we append() that value to it!
Then be sure to return that output list outside of the loop, otherwise your code will stop early!

7 Likes

def remove_duplicates(lst):
  est = []
  for i in lst:
    for c in est:
      if i not in c:
        est.append(i)
  return est

why does the above code not run?

Also,

def remove_duplicates(lst):
  est = []
  for i in lst:
    if i not in est:
      est.append(i)
  return est

Why does the above code run?

1 Like

The answer does even use the a not in b syntax suggested in the hint.
I came to your first solution by myself as well, but indeed it throws the error: “TypeError: argument of type ‘NoneType’ is not iterable”

est is an empty list, to start out. Examine the inner for loop. On its first run, what will c be? You got it, None.

Now we see why the above error was thrown. We cannot use not in None since in expects an iterable.

est, on the other hand is iterable, even if empty. Now not in can be executed and will yield True if i is NOT in the list, or False otherwise. When true, append i to the list. The resulting list will have no duplicates.

Thank you for the explanation. My mistake was to use append as:

est = est.append(i)

which resulted to the same error.

1 Like
def remove_duplicates(list):
  z = []
  for x in range(len(list)):
    if x not in z:
      z.append(x)
  print z
  return z
    
    
y = [4, 5, 5, 4]
remove_duplicates(y)

Bit weird, because my code is basically what was suggested as the answer (based on reading wikibot), and it gave

remove_duplicates([4, 5, 5, 4]) returned [0, 1, 2, 3] instead of [4, 5]

In the above, x is acting as an index, not a value. All you are doing is appending the index. There will be no duplicates in a range.

for x in lst:

In the above, x is acting as a value in the list. There may be duplicates found amongst the values.

1 Like

ah, I did it.

def remove_duplicates(list):
  z = []
  for x in range(len(list)):
    if list[x] not in z:
      z.append(list[x])
  print z
  return z
    
    
y = [1, 2, 3, 4, 4]
remove_duplicates(y)

Thank yee.

3 Likes
def remove_duplicates(lista):
  apot = lista
  apot.sort()
  mikos = len(apot) - 1
  for i in range(mikos, 0, -1):
    print'i=', i, 
    if apot[i] == apot[i-1]:
      apot.pop(i)
    print apot

  return 
​
remove_duplicates([3, 0, 1, 7, 0, 2, 3])

After running the above, keeps displaying the following error msg:

"Your remove_duplicates modifies the list that was passed to it. [4, 5, 5, 4] was modified to [4, 5]. Perhaps you should make a copy first!"

Any comments?

The lista array is not copied to apot. Only a reference is assigned so sorting or moditying either will affect lista. They are not separate lists.

To copy a list take a slice.

apot = lista[:]

or use the copy() method.

apot = lista.copy()

I didn’t really check the solution, but I’m curious about how my code would fare compared to it:

def remove_duplicates(le):
  copy_list = le[:]
  new_list = []
  duplicates_list = []
  a = 0
  b = 0
  c = 0
  for a in range(0,len(le)):
    for b in range(a+1,len(le)):
      if le[a] == le[b]:
        duplicates_list.append(le[b])
        if len(duplicates_list) > 1:
          for c in range(0,len(duplicates_list)-1):
              if duplicates_list[c] == duplicates_list[c+1]:
                duplicates_list.pop(c+1)      
  for item in copy_list:
    if item not in duplicates_list:
      new_list.append(item)
  for item in duplicates_list:
    new_list.append(item)
  return new_list

It just feels like I wrote so much code to do so little in a language where everything seems to be done in few lines :sweat:

Would someone more experienced be able to tell me where am I wasting time on these types of code? Thanks in advance :wink:

What message did you get when submitting your exercise code?

def median(order):
new_lst =
new_lst.append(order.sort())
for i in range(len(new_lst)):
mid = len(new_lst) / 2
return new_lst[mid]
if len(new_lst) % 2 == 0:
first_i = new_lst[mid]
sec_i = new_lst[mid + 1]
find_med = first_i / sec_i
return find_med

print median([1])
print median([1, 2, 3])
print median([4, 5, 5, 4])

I don’t know why the code above doesn’t work. Does anyone know where I went wrong?

I am just curious as to why this is how codecademy did the assignment. The code listed below is what shows up.

def remove_duplicates(inputlist):
    if inputlist == []:
        return []
    
# Sort the input list from low to high    
    inputlist = sorted(inputlist)
# Initialize the output list, and give it the first value of the now-sorted input list
    outputlist = [inputlist[0]]

# Go through the values of the sorted list and append to the output list
# ...any values that are greater than the last value of the output list
    for i in inputlist:
        if i > outputlist[-1]:
            outputlist.append(i)
        
    return outputlist

Is this a more efficient way of doing it? My code works perfectly and is listed below.

def remove_duplicates(lst):
  result = []
  for i in lst:
    if i not in result:
      result.append(i)
  return result

n = [1, 1, 2, 2, 3, 3, 4, 4]
print remove_duplicates(n)

Please correct me if I am missing something.

Two things remain unchanged in your code (which is a good thing)…

  1. The order of the original list is preserved.
  2. The order of the output list matches the input list.

The author of the solution might think it clever to sort and compare the last element to the item in question, but the outcome is a sorted list, and the original list is reordered.

def remove_duplicates(chaos):
  once_only = [] 
  for digits in chaos:
   if digits not in once_only:
    once_only.append(digits) 
   return once_only 

print remove_duplicates([1, 1, 2, 3, 2, 3, 4, 5])

the answer keeps appearing as [1]
Why are the other digits not following?

Your indentation seems a little funny to me. Even on codeacademy the standard is two whitespace characters for code blocks and four is the standard for many styles. A single whitespace is asking for trouble. As for the issue, remember what return does in a function. When a return statement is executed the entire function ends.

2 posts were split to a new topic: Can someone tell me why this works

Hello @thien1892 and welcome to the Codecademy Forums.

Is there a specific question you want to ask related to your code? Solutions to exercises should not be posted on the forums.

1 Like

sorry, i will delete