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

Python pop value question from Kenneth's "Lists" video

Hello,

Another day, another question...

In the Python Collections module when Kenneth goes over "pop", at about the minute marker in the video he shows/explains how pop works in relation to the list:

names = ['Kenneth', 'Joy', 'Sam']

name_1 = names.pop()

name_1 <---will now output:

'Sam'

names <---will now output:

['Kenneth', 'Joy']

I don't understand why 'Sam' is popped. In this case, does pop pop-out the item from right to left? The next explanation of using pop with an index attached made perfect sense.

Thanks!

3 Answers

Yes, if using pop() without an index, it will pop out the last item in the list. If you want even more info, check out the Python documentation here.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

pop() without an index removes and returns the last item in the list. From Python docs:

list.pop([i])

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.

A list can sometimes be used as a "queue" where items are added to the left and removed from the right. See also collections.deque (mentioned in the same link above), a structure that adds a popleft() and a popright() method.

thanks guys! i'm being asked for a best answer and since bryan was the first to reply, it seems the most diplomatic to choose it based on that. i appreciate the help!