Python Pop Exercises

Let’s check out some exercises that will help understand .pop() method better.

Exercise 12-a: Python pop method of lists and dictionaries

Pop the last item of the list below.

.pop() method can be used to remove an item of a list or a dictionary and return that value. So, you can assign it to another variable if you’d like.

If you don’t put anything inside .pop() method’s parenthesis it will automatically pop the last item in a list.

However, you can’t do this for dictionaries since they don’t have index orders.

popped_item = lst.pop()

Exercise 12-b: Python pop method to remove last list item

Remove "broccoli" from the list using .pop and .index methods.

.index() method can be helpful to find the index of the item you’re looking for.

You can pass the index number of the element to the .pop() method and it will remove it.