Answers for "how to use .pop in python"

14

python pop element

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)
Posted by: Guest on March-23-2020
3

python list pop

# Python pop list

some_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # normal python list
print(some_list) # prints [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

some_list.pop() # pop() is used to pop out one index from a list (default index in pop is -1)
print(some_list) # prints [1, 2, 3, 4, 5, 6, 7, 8, 9]

=====================================================
# Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Posted by: Guest on January-10-2021

Python Answers by Framework

Browse Popular Code Answers by Language