# Lists Practice Problems - #11

New to the discussion board (and Python!) so please bear with me.
Working through the practice problems for lists and can’t figure out the problem with my code.

I took an admittedly more complicated route than the suggested solution, but I still get the correct answer; however when I submit, I get the error “Function remove_at_indx did not return the expected value”

Help?

Problem
Create a function, remove_at_idx , with the following features:

• Takes a list as its first argument.
• Takes a positive index as its second argument.
• Removes the element at the given index and decreases the index of all subsequent elements by one.
• Returns a new list.

My Code

def remove_at_idx (xlist,index):
ylist = [  ]
for item in xlist:
indexcheck = xlist.index(item)
if index != indexcheck:
ylist.append(item)
return ylist

remove_at_idx(["A", "B", "C", "D", "E"], 3)

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

Hey, Megan.

Here’s a hint. Investigate what happens when you run your function with the following two inputs:

• ["A", "A", "A"], 1
• [0, 1, 2, 2, 2, 2], 2

Solving it in your head, what should be the output of each of them? What is the actual output?

Hi Bruno,

I can see now that duplicates obviously throw my code off.

I misunderstood the list.index function to take the index of each iteration, rather than the lowest index it found of that value.

I’m sure there is a workaround but probably too advanced for me at this stage

Thanks for pointing me in the right direction!

Hey, Megan.

I don’t think this is beyond you at this stage, not given that you were able to understand where your code was going wrong.

Here’s an idea:

• Create an empty list.
• Loop through the input and do something at each iteration (except for one of them)

Although it isn’t necessary, the enumerate function can be useful here.

hi @Bruno, please i need more explanation on this response. thanks

My answered here was tailored to Megan. It’s hard to give you helpful information without knowing what you’re having trouble with.

This is a very instructive mistake to make. As Bruno suggests

for i, item in enumerate(xlist):

Would have done what you wanted. I find for myself that often after spending a long time on a problem that Python has a way to do it already. In this case you can do the following

xlist.pop(index)

This is not as instructive but does demonstrate the power of Python I think.

3 Likes