# 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']
my_list = [-15, 0, 67,45]
print(my_list) # [-15, 0, 67,45]
my_list.pop(3) # 45
print(my_list) # [-15, 0, 67]
#A negative index is an index that is numbered from the back of a list, instead of from the front.For example, the index -1 refers to the last item in a list, while the index -2 refers to the second-last item in a list.The pop() method can be used to delete an item in a list using a negative index.
my_list.pop(-1) # 0
print(my_list) # [-15,0]
# 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]
Remove an element from a Python list Using pop() method
List = [1,2,3,4,5]
# Removing element from the
# Set using the pop() method
List.pop()
print("
List after popping an element: ")
print(List)
# Removing element at a
# specific location from the
# Set using the pop() method
List.pop(1)
print("
List after popping a specific element: ")
print(List)
In [1]: from typing import List, Any
In [2]: x = [{"count": 100}, {"count": 30}]
# use this function
In [3]: def defaultable_pop(input_list: List, index: int, default_value: Any = None) -> Any:
...: try:
...: return input_list.pop(index)
...: except IndexError:
...: return default_value
...:
# list, index, (default of empty dict)
In [4]: y: int = defaultable_pop(x, 0, dict({})).get("count", 0)
In [5]: y
Out[5]: 100
# As opposed to:
# y: int = x[0].get("count", 0) if len(x) > 0 else 0