+ 2

Pop method in python

A=[1,2,3,4] Last =A.pop() print(Last) Why am I getting 4 and not a list[1,2,3] Because pop removes the element and the list stays

1st Jul 2019, 5:49 AM
I Am Anushka
3 Respostas
+ 8
List.pop() method is used to remove and print the last element of the list. Now A= [1,2,3,4] Last = A.pop() Now A.pop() returns the last element and thereafter removes it. So Last = 4 And then print(Last) prints 4 Thanks
1st Jul 2019, 5:54 AM
Prince PS[Not_Active]
Prince PS[Not_Active] - avatar
+ 5
If there are any items in the list and if you use no additional arguments, list.pop method does 2 things: list.pop removes the last item from the list in-place and returns the removed item.
1st Jul 2019, 6:52 AM
Seb TheS
Seb TheS - avatar
+ 3
Run this code and you will see that '4' is removed: A=[1,2,3,4] Last =A.pop() print(A)
1st Jul 2019, 8:02 AM
Paul
Paul - avatar