Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,389 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,022 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,389 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