TheDeveloperBlog.com

Home | Contact Us

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

Python List remove() method with Examples

Python List remove() 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 remove(x) Method

Python remove() method removes the first item from the list which is equal to the passed value. It throws an error if the item is not present in the list. Signature and examples are described below.

Signature

remove(x)

Parameters

x : Element to be deleted.

Return

It does not return any value rather modifies the list.

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

Python List remove() Method Example 1

A simple example in which we are removing an element from the list.

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

Output:

1
2
3
After removing:
1
3

Python List remove() Method Example 2

If list contains duplicate elements, the method will remove only first occurred element. See the example below.

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

Output:

1
2
3
2
After removing:
1
3
2

Python List remove() Method Example 3

If we remove an element which is not found in the list, it throws an error to the console. See the example below.

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

Output:

ValueError: list.remove(x): x not in list
1
2
3
2

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