Friday 4 December 2015

Python: list: pop: remove last element in the list

list.pop([i])
‘pop’ method without argument removes last element in the list. If you specify the position ‘i’, it removes the element at position ‘i’ and return it.

test.py

list=[2, 4, 6, 6, 4, 2]
print(list)

print("Removing last element ", list.pop())
print(list)

print("Removing element at index 2 ", list.pop(2))
print(list)


$ python3 test.py
[2, 4, 6, 6, 4, 2]
Removing last element  2
[2, 4, 6, 6, 4]
Removing element at index 2  6
[2, 4, 6, 4]


Previous                                                 Next                                                 Home

No comments:

Post a Comment