thanby
0
Q:

pop function python

my_list = [123, 'Add', 'Grepper', 'Answer']
my_list.pop()
-->[123, 'Add', 'Grepper'] #last element is removed

my_list = [123, 'Add', 'Grepper', 'Answer']
my_list.pop(0)
-->['Add', 'Grepper', 'Answer'] #first element is removed

my_list = [123, 'Add', 'Grepper', 'Answer']
any_index_of_the_list = 2
my_list.pop(any_index_of_the_list)
-->[123, 'Add', 'Answer'] #element at index 2 is removed 
						  #(first element is 0)
11
# Python list method pop() removes
# and returns last object or obj from the list.
# .pop() last element .pop(0) first element

my_list = [123, 'Add', 'Grepper', 'Answer'];
print "Pop default: ", my_list.pop()
> Pop default:  Answer
# List updated to [123, 'Add', 'Grepper']
print "Pop index: ", my_list.pop(1)
> Pop index: Add
# List updated to [123, 'Grepper']
17
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item
return_value = languages.pop(3)
print('Return Value:', return_value)

# Updated List
print('Updated List:', languages)
5
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
#--> ['apple','cherry']

fruits = ['apple', 'banana', 'cherry']
fruits.pop()  # Default is -1 (last item)
#--> ['apple','banana']
1
#pop removes the last element
li=[1,2,3,4,5]
li.pop()
#>>>[1, 2, 3, 4]
2

New to Communities?

Join the community