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 Collections (Retired) Lists Redux Pop

Emmet Lowry
Emmet Lowry
10,196 Points

how do I know what I have removed from the list with pop

In the pop video you assign a name to the variable name_1. It returns Sam . how do I know what is being removed from the list or is it just a random process. Thanks for any help

5 Answers

Dan Johnson
Dan Johnson
40,533 Points

If you don't specify an index with pop it will remove the last element in the list:

# Will return 3
[1,2,3].pop()

If you do pass in an index it'll behave as expected:

# Will return 2
[1,2,3].pop(1)
Emmet Lowry
Emmet Lowry
10,196 Points

Thank you Dan that wasnt very well explained in the video. One other question what does an index refer to?An item inside the bracket. and how would i insert a item with insert back into a list thx sry for the questions.

Dan Johnson
Dan Johnson
40,533 Points

Yep, it's as if you were selecting it with the brackets:

my_list = [1,2,3]

print("Element at index 1: {}".format(my_list[1]))
print("Popping index 1: {}".format(my_list.pop(1)))

# my_list will now be [1,3]
print("my_list: {}".format(my_list))

I feel really dumb. which video is the pop video? i'm going back through them and I don't remember seeing it before, and if I did...I can't find it again.

well, i feel silly but thanks!

Herb Bresnan
Herb Bresnan
10,658 Points

Thank you Dan. I think he skipped over the part where .pop() removes the last item if not specified.