Pop() list Python3 | Sololearn: Learn to code for FREE!

0

Pop() list Python3

fruits = ['apple','banana','cherry'] x = fruits.pop(1) Print(x) Output: cherry And fruits = ['apple','banana','cherry'] fruits.pop(1) print(fruits) Output: ['apple','cherry'] Why is it showing different results although the code looks slightly similar? Please explain

7/2/2021 12:33:30 PM

Martial Law

3 Answers

New Answer

+6

The first doesn't output cherry, but rather banana, which is the returned value from the pop() at index 1 that is saved into the variable x. The second doesn't save the value, but it is still removed from the fruits list. Which is then output.

+3

Are you sure about output? Can you check it again...

0

In the first you print x and in the second you print fruits, thats the difference.