How to Use List pop() Method in PythonPython Basics


The pop() function removes an element from the list at the specified index(the default is the last element).

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

list1 = ['MIT', 'CIT', 'Yale','UCI']
print("the list1 is: ",list1)

list1.pop()
print("the new list: ", list1)

list1.pop(1)
print("the new list: ", list1)

Output:

the list1 is:  ['MIT', 'CIT', 'Yale', 'UCI']
the new list:  ['MIT', 'CIT', 'Yale']
the new list:  ['MIT', 'Yale']

Syntax

list.pop([index=-1])

Parameters

Name Description
index The pop() method takes a single argument( an index) and removes the element present at that index.

Return Value

It returns the element present at the given index.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us