TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python List pop() method with Examples

Python List pop() method with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python List pop() Method

Python pop() element removes an element present at specified index from the list. It returns the popped element. The syntax and signature is described below.

Signature

pop([i])

Parameters

x : Element to be popped. It is optional.

Return

It returns popped element.

Let's see some examples of pop() method to understand it's functionality.

Python List pop() Method Example 1

Let's first see a simple example of popping element from the list. Element present at the index 2 is popped, see the example below.

# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list.pop(2)
print("After poping:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After poping:
1
2

Python List pop() Method Example 2

The index is optional, if we don't specify the index, it pops element presents at the last index of the list. See the example below.

# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list.pop()
print("After poping:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After poping:
1
2

Python List pop() Method Example 3

If the index is a negative value, it will pop element from the right of the list. See the example below.

# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list.pop(-2)	# Item will be removed from the right of the list
print("After poping:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After poping:
1
3

Next TopicPython Lists




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf