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.


Exercise 12-c: Pop method also saves the item being removed

Save Italy's GDP in a separate variable and remove it from the dictionary.

In dictionaries, .pop() method will still work but it will require you to write the name of the key inside it.

italy_gdp = GDP_2018.pop("Italy")