Remove_duplicates, using sorted to filter & append


#1



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


Error = Oops, try again. remove_duplicates([4, 5, 5, 4]) resulted in an error: 'int' object has no attribute '__getitem__'


So with the [4,5,5,4] from the error, I would think that the sorted() would change the input list to [4,4,5,5], (having learned in another Q&A that .sort is not the right way to go, as it would return nothing.)

...then the output list would be initialized with the value 4. I've tried wrapping the initialization with list() in order to specifically call out that I want 4 to be the beginning of a list, but it only creates a different error.

...then the loop would start through the input list and compare each input list value to the final value of the output list. So discard the initialized 4, the second 4, and when it arrives at the first 5, a-ha!, a value that is greater than 4, so append to output list.

...ignore the final 5, as it is not greater than the 5 just appended. Then return output list.

Thank you in advance.....I feel like I'm close here, but am missing an important distinction that I should be learning...


def remove_duplicates(inputlist):
    
# 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 = outputlist.append(i)
        
    return outputlist


#2

What value type will outputlist have?

Does that operation make sense for that value type?


#3

Wow, thanks for the quick reply!!

So yeah, I considered that, and replaced initialization with:
outputlist = list(inputlist[0])
...in attempt to say, "This outputlist variable is a list!"

...and instead received the error:
Oops, try again. remove_duplicates([4, 5, 5, 4]) resulted in an error: 'int' object is not iterable

I also tried:
outputlist = [inputlist[0]]

...and the error became:
Oops, try again. remove_duplicates([4, 5, 5, 4]) resulted in an error: 'NoneType' object has no attribute '__getitem__'

....but I can't quite figure out why none of these are working. My syntax is off somehow....


#4

list() can only cast iterable items to a list, integers are not iterable items

see list documentation


#5

Oooookay, this seems a bit circular to me; I have a single-digit integer, I want a list with one value. But I can't use list() as that can't cast integers. I can see you're wanting me to work for this. I can appreciate that. :relaxed:

So then I'll ask about my backup idea. Why does outputlist = [inputlist[0]] also not work?

Thank you for your reply, @stetim94!


#6

it works, except this line:

outputlist = outputlist.append(i)

append will append to outputlist, no need to update the variable as well


#7

Ok, fixed that, and then turns out the code needed to address an input of [], and so I then threw in my first error handler at the top and it worked!

Marking this solved, but if anyone finds my error handling to be substandard/attrocious (code below), I'm all ears on any additional feedback!

Thanks again @stetim94!

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

#9

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