I meet Problems in Loops. Hope someone can tell me what's wrong with my code😟

def remove_duplicates(list):
  result = []
  for i in list:
    list_remove = list.remove(i)
    if i not in list_remove:
      result += i
  return result
  
remove_duplicates([4,5,5,4])

I can’t believe the code runs wrong again and again but it only told me ā€œargument of type ā€˜NoneType’ is not iterableā€.

What’s going wrong?? Can anyone tell me the riddles?

Ps: sorry for my English expression for it’s not my mother tongue language.

2 Likes

It’s because of trying to remove items from the list you are looping through.

You can create a copy of the list like this, and also use .append() to add i

def remove_duplicates(list):
  result = []
  list2 = list.copy()
  for i in list:
    list2.remove(i)
    if i not in list2:
      result.append(i)
  return result
  
remove_duplicates([4,5,5,4])

Hope that helps!

1 Like

Thank you so much!

I haven’t learned the copy() function. Maybe it’s in the subsequent classes.

Your answer helps me a lot and your code is clear and beautiful. (:heart: ω :heart:)

1 Like

Thank you!! I found copy() with a web search, but it might be in subsequent classes :slight_smile:

It could be coded without copying the array too like this:

def remove_duplicates(list):
  result = []
  list.sort()
  for i in list:
    if i not in result:
      result.append(i)
  return result
  
remove_duplicates([4,5,5,4])
1 Like

Oh, you give me another way to think about this problem. It’s so kind of you!!

I’ll look for the copy( ) on the Internet and think about your new solution~ :smiling_face_with_three_hearts:

1 Like

I’d avoid sorting the value passed. If you’re returning the filtered version, why should I expect value passed to be mutated? With this in mind, shouldn’t the result be in the same order as the source, sans dups?

def remove_duplicates(xs): # list is a horrid name for a var in python
    result = []
    for x in xs:
        if x not in result:
            result.append(x)
    return result

print(remove_duplicates([5,4,4,5]))

You can actually get kind of clever with this, if you’re inclined, using a list comprehension.

def remove_duplicates(xs):
    return [x for (idx,x) in enumerate(xs) if x not in xs[:idx]]

Note, that list comprehension can be used as a straight up copy [x for x in xs].

It occurs to me that you could do a silly, but interesting, a sieve of eratosthenes kind of thing, and avoid the in check.

def remove_duplicates(xs):
    sieve = [x for x in xs] # make that copy
    for i in range(len(xs)):
        if sieve[i] is not None:
            # replace all other occurances with None
            for j in range(i + 1, len(xs)):
                if sieve[j] == sieve[i]:
                    sieve[j] = None
    return [x for x in sieve if x is not None] # return the not None

While you pay for the copy up front, I suspect this would be the quicker one on larger sets…

Sets?

Ok, we’d be remiss if we didn’t mention the python built in for this; order not maintained.

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

The above also helpfully shows why list is a bad idea for a variable name. Never shadow function names, it will only cause pain and suffering.

3 Likes

Thanks so much for all this! :pray: that’s a good point about not sorting and the using a different name than ā€œlistā€.

I hadn’t heard of the sieve of Eratosthenes, will have to look it up :slight_smile:

1 Like

Thanks again for reminding list is an unsuitable variable name. I’ve tried Sieve but I failed :sweat_smile:. I’m happy to receive your solution :blush:.

I tasted your code again and feel it’s so ingenious with clearer logic. Good job :+1:

1 Like

So happy I could help! :slight_smile: It’s also helping me learn by thinking about how to code it :pray:

1 Like

I’ve learned more knowledge about Python Syntax and I can better understand your answer. You are so humorous and thank you for providing several solutions as well as pointing out a bad habit of coding. Wish you happy :sunny:

1 Like