Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Technical Interview Prep: Python Basics Basic Python Once You Pop

Leslio McKeown
seal-mask
.a{fill-rule:evenodd;}techdegree
Leslio McKeown
Python Development Techdegree Student 9,439 Points

Create a function called pop. It will take 2 parameters, a list and an index.

Bummer: Uh oh, I didn't get the correct value returned. It should have been [5, 6, 7, 9]. Instead, it was 8

pop.py
def pop(num_list, index):
    try:
        return num_list.pop(index)
    except IndexError as error:
        return ('Invalid Index')

1 Answer

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,409 Points

Hi Leslio!

This code:

   return num_list.pop(index)

... is telling Python to return the value that gets popped/removed from the list.

That's why your function is returning 8, and not the list [5, 6, 7, 9]

You still want to pop the index from the list. You just need to adjust your code to return the list- not the popped value.

The next time you call num_list, Python will remember that you popped the index (in this case, 8) from it. That value won't exist in the list. Try updating your code based on that!

Leslio McKeown
seal-mask
.a{fill-rule:evenodd;}techdegree
Leslio McKeown
Python Development Techdegree Student 9,439 Points

Thank you so much! i changed it to:

def pop(num_list, index): try: num_list.pop(index) return (num_list) except IndexError as error: return ('Invalid Index')

and it worked