Pop() list Python3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

2nd Jul 2021, 12:33 PM
ORAL NAPIER
ORAL NAPIER - avatar
3 Answers
+ 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.
2nd Jul 2021, 12:47 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Are you sure about output? Can you check it again...
2nd Jul 2021, 12:47 PM
Jayakrishna 🇮🇳
0
In the first you print x and in the second you print fruits, thats the difference.
2nd Jul 2021, 2:02 PM
Angela
Angela - avatar