Remove_duplicates


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096


I dont understand whats wrong.


def remove_duplicates(lis):
    slit=[]
    lis.sort()
    for i,j in enumerate(lis):
        if lis[i]!=lis[i+1]:
            print j
            slit.append[j] 
    return slit
print  remove_duplicates([4, 5, 5, 4])


#2

append() is a method, calling a method requires parentheses, square brackets is for accessing strings and list by index

the list should remove all duplicates, so for example:

[1,2,3,4,5,1]

you should get [1,2,3,4,5], so only to check if the next value doesn't match the current value is not enough


#3

but I am sorting the list first, so all the same values will come next to each other


#4

i missed that, clever

still the problem of append()

also, i+1 will also cause problems for the last item in list, given i+1 is then an index error


#5

yes i+1 will access index which are not in the lists scope, I am thinking of a way around it


#6

hey, so I made those changes and I am recieving this error now,
MODIFIED CODE

def remove_duplicates(lis):
    slit=[]
    lis.sort()
    count=0
    for j in lis:
        if count<=len(lis)-1:
            if lis[count]!=lis[count+1]:
                print j
                slit.append(j)
            count+=1
        else:
            slit.append(lis[count])
        return slit
print  remove_duplicates([4, 5, 5, 4])

RECIEVED ERROR:

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


#7

I think the problem is lis and lis.sort(). using for j in lis, you provide python the lis, not lis.sort() (and list.sort() also is unsuitable), so that your code will not be right. I think you should replace:

lis.sort()
for j in lis:

by

new_lis = sorted(lis)
for j in new_list:

or no need to have sorted(lis) separately, just straightly input:

for j in sorted(lis):

and more simple, just try mine :smile:

def remove_duplicates(my_list):
    new_list = []
    for i in my_list:
        if i not in new_list:
            new_list.append(i)
    return new_list
    
print(remove_duplicates([4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]))

Hope you can resolve this.


#8

this does not work it flags off the following error:


Traceback (most recent call last):
File "python", line 15, in
File "python", line 6, in remove_duplicates
TypeError: 'NoneType' object is not iterable


Also your code seems super easy, though I'd like to make mine work, despite being more complex, I just need to believe in my logical ability, I hope you understand.


#9

Oops, I know man, I 've made some changes in my upper reply, please consider and solve it by your way:slight_smile:


#10

I noticed, the changes work. I'll probably look at the code tomorrow morning , with fresh eyes. Thank you for your help so far. You'll defiitely be the first to know when I have successfully debugged my code. :slight_smile:


#11

maybe you should make a copy first then? shouldn't be that difficult

why? .sort() is a method specifically for list, manipulating the list directly rather then returning a new list, which is more efficient then a general sorted() function

The trick is to design an efficient program, but now you have progressed so far, you should make your own code work

we could use sorted() to our advantage, given it will make a copy of the list


#12

Hi there,
I think you've made your code more complicated than it needs to be. I was also getting the TypeError:'NoneType' object is not iterable error and it was super frustrating.
I believe this error exists as a consequence of the lis.sort(). In the instructions, it says to NOT modify the input list, which you do by using this function.
Try using the a not in b syntax suggested in the hint section of the exercise. This is the method I used and it turned out well. :slight_smile:


#13

Yes I had thought of a logic before looking at the hint and then post that I just became adamant on making it work. I have relented now and thought of a different logic and that worksfine. :blush: Thanks for the suggestion!


#14

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