0

What is use of pop function?

16th Feb 2019, 4:15 AM
Ankush Mahto
Ankush Mahto - avatar
3 ответов
+ 5
Pop function works with lists in python List can have number of members. Now let's take a example to help you to understand pop function We have a list list1 = [1,2,3,4,5,6,7] Now we want to remove 4 from list and want to store it in another variable So what we will do, we will see what is index of 4 in list1 Index of 4 in list1 is 3 temp = list1.pop(3) It will remove the member at index 3 which is 4 and will store in temp variable Run this code in playground list1 = [1,2,3,4,5,6,7] temp = list1.pop(3) print(list1) print(temp)
16th Feb 2019, 5:31 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 6
list.pop(i) removes and returns item at i (or last item if i isn't specified) set.pop() removes and returns random item from set dict.pop(k, d) removes k and returns dict[k]. If k isn't found it returns d or raises KeyError if d isn't specified
16th Feb 2019, 8:33 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 5
set.pop gives you a "random" or at least uncertain value from the set. You may use it if you want to consume values from a set and you don't care in which order. If the order is important then you should use a list instead.
16th Feb 2019, 8:54 AM
Tibor Santa
Tibor Santa - avatar