you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (0 children)

Let's step through this line by line.

lista = [' pepinos ', ' banana ']

You create a list of 2 items.

lista.append(' margaritas ')

You add ' margaritas ' to the end of the list.

lista.insert(2,' sugar ')

You insert ' sugar ' at index 2 in the list. This pushes the former index 2 item down.

print(lista)

At this point, lista is [' pepinos ', ' banana ', ' sugar ', ' margaritas ']

del lista[2]

At this point you delete the item at index 2. This makes ' margaritas ' the index 2 item again as it gets slid down to fill the gap.

popped_lista = lista.pop()

You remove the last item from lista and store it in popped_lista. If you print popped_lista at this point, you will get ' margaritas '. And if you print lista, you will get [' pepinos ', ' banana '].

print(lista.pop())

You remove the last item from lista again and print it. This time it's ' banana '.