Remove_duplicates


#1
def remove_duplicates(a):
  b = []
  for i in a:
    for j in b:
      if i != j:
        b.append(i)      
  return b

What is wrong here? Output is coming as an empty list


#2

b is an empty list
Why would you compare the array you are given to an empty list?


#3

what is wrong in doing so? I mean if we are equating an element with a null one, it should return False. Shouldn’t it?


#4

You are given a basket of fruits
If there is more than one of a type of fruit, remove it.

Logically Should you
A. Take one fruit out of the basket, compare it to the rest of the fruits in the basket.
or
B. Take a fruit out of the basket and compare the fruit to an empty basket


#5

can you post your code here to understand this a little better?


#6

Then we would waste a valuable learning opportunity. Problem solving is one of the most (if not the most) important aspects of programming.

the double loop like you have it:

  for i in a:
    for j in b:

is a possible approach. I think its fight_dragons plan B.

But you are not complete there, lets add a function call so we can see the function in action:

def remove_duplicates(a):
  b = []
  for i in a:
    for j in b:
      if i != j:
        b.append(i)      
  return b

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

we can see an empty list, given b is empty, so the second for loop never runs.

This is because the comparison in your loop is no good. We only need to append i to the list after we compared all values in empty basket (second for loop)


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.