python list pop equivalent code example

Example 1: python pop

# 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']

Example 2: pop list python

#pop removes the last element
li=[1,2,3,4,5]
li.pop()
#>>>[1, 2, 3, 4]

Example 3: python list pop equivalent

# Python pop() equivalent
mylist = ['a', 'b', 'c', 'd', 'e']
# remove 'b'
mylist = mylist[:1] + mylist[1+1:]
# return ['a', 'c', 'd', 'e']
A = 0
A, mylist = A + 9, mylist[:1] + mylist[1+1:]
# return 9 ['a', 'd', 'e']

Example 4: python list pop

# removes last element of list unless parameter is given
l1 = [1,2,3,4,5]
print(l1.pop()) # returns [1, 2, 3, 4]
print(l1.pop(2)) # returns [1, 2, 4, 5] removes element at given index

Example 5: list pop python

list.pop(index)