Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the list remove() function in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The remove() method is a built-in method in Python that removes a specified element in a list. If the specified element is present more than once, its first occurrence is removed from the list.

Syntax

The syntax of the remove() method is as follows.

list.remove(element)
The syntax of remove() method in Python

Parameters and return value

This method takes one necessary parameter that needs to be removed from the list.

The remove() method does not return a value. However, if the specified element is not present in a list, it gives a value error.

Code

In the code below, the element e is removed from the list vowels. In the next attempt to remove s from the list, the function gives a value error because s is not present in the vowels list.

# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
# 'e' is removed by remove() funtion
vowels.remove('e')
# Updated vowels List
print('Vowels list: ', vowels)
# 's' ihas to be removed from vowels: Value error
vowels.remove('s')
print('Error Vowels list: ', vowels)
The code example of remove() method in Python

In the number list, 6 is present twice and the remove() function removes its first instance.

# number list
number = [1, 2, 3, 6, 4, 5, 6]
# '6' is removed by remove() funtion
number.remove(6)
# Updated number List
print('Number list: ', number)
The code example of remove() method in Python

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring