Lists - Practice Problem #11 - remove_at_idx

Screen Link:
https://app.dataquest.io/m/1018/lists/11/removing-from-the-queue

My Code:

def remove_at_idx(some_list, idx):
    return some_list.remove(some_list[idx])

What I expected to happen: When I run the code against test lists, it seems to consistently return the correct solution and remove the list item with the index that I’ve specified.

What actually happened:

Function remove_at_idx did not return a value with the expected type. We expected list but got NoneType instead.

2 Likes

list.remove function does not return any value. It make changes in list object inplace. In python if function does not return any value returns explicit None that is NoneType object.

but if you required to return list any way it could be

def remove_at_idx(some_list, idx):
    some_list.remove(some_list[idx])
    return some_list

But it can lead you to another problem when list has duplicates items.
So in this case it fails

>>> print(remove_at_idx(["A", "B", "C", "B", "E"], 3))
['A', 'C', 'B', 'E']

It suppose to delete 3 index item instread it did 1. Because list.remove - “Remove the first item from the list whose value is equal to x .”

Hence, we can not use list.remove method for this use case here. Look for other ways slicing or del.

2 Likes

@DishinGoyani I also faced the same problem and tried your solution but when I click ‘submit answer’ it gives me an error:

Function remove_at_idx did not return the expected value.

2 Likes

Yes i have just updated possible cause of that.

Hi @colleen.mccaskell I have found a solution to your problem

You should use del instead of remove as del allows you to remove items by index

def remove_at_idx(some_list, idx):
    del some_list[idx]
    return some_list

Hello @colleen.mccaskell, Welcome to the community.

remove() is an inbuilt list method in Python programming language that removes a given object from the list . It does not return any value. In simple terms is that it removes the object inplace.
Python Lists has many methods, check the documentation

There are many ways to achieve what you want. Since in your function remove_at_idx you are passing the list and the index, the best method I would use is to use the .pop() list method and return the list

def remove_at_idx(ls, idx):
    ls.pop(idx)
    return ls
2 Likes

Thanks, Dishin.

Just trying to wrap my head around this one – so basically, the instructions are to take a list and index as the arguments, remove the element at the index and return a new list. The problem with list.remove is that it changes the original list and therefore DOESN’T return a new list – just a NoneType object.

I think the above makes sense, I’m just trying to better understand why the del and .pop() methods DO work with this task as they both modify the original list, as well.

Thanks for your time!

2 Likes

Thank you, this seems to work for me, as well!

Still trying to get a better understanding of the various list methods – del and .remove() are quite similar but I suppose the benefit here is that del won’t be affected by duplicate list items?

Thank you, this works for me, as well!

Would you say that .pop() is superior to del because the method itself is specifically designed to refer to an index and remove it? I suppose both methods ultimately accomplish the same thing, just curious :slight_smile:

1 Like

Yes that is right. And Second IT does not work with duplicate items. That is why we should not use it for our task.

They all same changes original list. But you have to pay attention to what they returns?

  • del is keyword does not return anything.
  • remove method return NoneType and
  • pop method return item that has been deleted.

So we can not use any of these three with return statement. Like in your code

return some_list.remove(some_list[idx])

Because we want to return new list from our function remove_at_idx(); None of above does that.

If you want to know more about del, remove and pop visit

2 Likes

Thanks again :slight_smile: