Remove Duplicates - Confused


#1

I know this isn’t the most elegant code, but I felt it should work. What am I doing wrong here?



def remove_duplicates(x):
  dup = []
  new = []
  final_list = dup + new
  for i in x:
    if x.count(i) > 1:
      dup.append(i)
    else:
      new.append(i)
  return final_list



#2

ok nvm i solved it - I was over thinking :joy:

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

#3

Just a side note, list is a built in function, and even though this is a local variable it is still better that we avoid using it, or any other function names or reserved words.


#4

Good point - thanks!


#5

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