remove() function in python by R4R Team

- remove() is python in-built function which is only work in case of the list data type.
- it remove the selected item from the list

Syntax-

list.remove(item)

- Here list refer the actual list in which we perform this operation
- item refer the element of list which we want to delete from list.

Example

list is [1,2,3]
l.remove(2) will give [1,3]


program-

l=[1,2,3,"one","two","three"]
print("List is",l)
l.remove(3)
print("Now list is ",l)

l.remove("one")
print("Now list is ",l)

l.remove(1)
print("Now list is ",l)

l.remove("two")
print("Now list is ",l)


output-

List is [1, 2, 3, 'one', 'two', 'three']
Now list is [1, 2, 'one', 'two', 'three']
Now list is [1, 2, 'two', 'three']
Now list is [2, 'two', 'three']
Now list is [2, 'three']



- if we try to remove that item which are not present in the list then program will give an error as shown below

program-

l=[1,2,3]
l.remove(4)
print(l)

output-

line 2, in
l.remove(4)

ValueError: list.remove(x): x not in list




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!