What is the difference between pop () and remove () function of list?

What Is Difference Between Del, Remove and Pop on Python Lists?

In python del is a keyword and remove(), pop() are in-built methods. The purpose of these three are same but the behavior is different remove() method delete values or object from the list using value and del and pop() deletes values or object from the list using an index.

del Keyword:

The del keyword delete any variable, list of values from a list.

Syntax:

del list_name[index] # To delete single value del list_name # To delete whole list

Example:




# program to demonstrate use of del keyword

# assign list

numbers = [1, 2, 3, 2, 3, 4, 5]

# use del

del numbers[2]

# display list

print(numbers)

# use del

del numbers[-1]

# display list

print(numbers)

# use del

del numbers[0]

# display list

print(numbers)

Output:



[1, 2, 2, 3, 4, 5] [1, 2, 2, 3, 4] [2, 2, 3, 4]

remove() Method:

The remove() method removes the first matching value from the list.

Syntax:

list_name.remove(value)

Example:




# program to demonstrate use of remove() method

# assign list

numbers = [1, 2, 3, 2, 3, 4, 5]

# use remove()

numbers.remove(3)

# display list

print(numbers)

# use remove()

numbers.remove(2)

# display list

print(numbers)

# use remove()

numbers.remove(5)

# display list

print(numbers)

Output:

[1, 2, 2, 3, 4, 5] [1, 2, 3, 4, 5] [1, 2, 3, 4]

pop() Method:

The pop() method like del deletes value at a particular index. But pop() method returns deleted value from the list.

Syntax:

list_name.pop(index)

Example:




# program to demonstrate use of pop() method

# assign list

numbers = [1, 2, 3, 2, 3, 4, 5]

# use remove()

numbers.pop(3)

# display list

print(numbers)

# use remove()

numbers.pop(-1)

# display list

print(numbers)

# use remove()

numbers.pop(0)

# display list

print(numbers)

Output:

[1, 2, 3, 3, 4, 5] [1, 2, 3, 3, 4] [2, 3, 3, 4]

del V/S remove() the V/Spop()

del

remove()

pop()

del is a keyword. It is a method. pop() is a method.
To delete value it uses the index. To delete value this method uses the value as a parameter. This method also uses the index as a parameter to delete.
The del keyword doesn’t return any value. The remove() method doesn’t return any value. pop() returns deleted value.
The del keyword can delete the single value from a list or delete the whole list at a time. At a time it deletes only one value from the list. At a time it deletes only one value from the list.
It throws index error in case of the index doesn’t exist in the list. It throws value error in case of value doesn’t exist in the list. It throws index error in case of an index doesn’t exist in the list.

What is the difference between pop () and remove () function of list?




Article Tags :

Python

Technical Scripter

python-list

Technical Scripter 2020

Practice Tags :

python-list

Difference between del, remove and pop on lists

In this article, we will learn the difference between the del, remove, and pop functions of a list in Python. We will use these built-in functions and discuss the difference with examples. Let's first have a quick look over what is a list in Python.

remove()

Thelist.remove(element)method removes the first occurrence of theelementfrom an existinglist. It does not, however, remove all occurrences of the element in the list!

Syntax: list.remove(value)
⦾ where, list is the name of your list.
⦾ value is the element that has to be removed.

Example: In the below list the element 2 is deleted from the list. But another occurrence of 2 is still in place.

li = [3, 5, 7, 2, 6, 4, 8, 2] li.remove(2) print(li) # [3, 5, 7, 6, 4, 8, 2]
  • remove() acts upon the value of an list element and not the index.
  • remove() is a method of the class list.
  • remove() method does not return anything.

If you specify a value that is not present in the list, Python raises ValueError.

li = [3, 5, 7, 2, 6, 4, 8, 2] li.remove(82) print(li) # ValueError: list.remove(x): x not in list

Complexity Analysis: The computational complexity when removing an element x from the list of n elements using the remove() method is O(n).

Case 1: To delete the first element from a list of 10000 elements using remove()

What is the difference between pop () and remove () function of list?

Note thatlist(range(10000)) generates a list with numbers 0 to 9999 something like [0,1,2,3,4,.......9999].

Case 2: To delete an element ‘5000‘ from the list of 10000 elements using remove().

What is the difference between pop () and remove () function of list?

Case 3: To delete the last element from a list of 100 elements using remove().

What is the difference between pop () and remove () function of list?

What is the difference between del, remove and pop on lists in python ?

Object Oriented ProgrammingProgrammingPython



It does't matter how many lines of code you write in a program. When you want to remove or delete any elements from the Python list, you have to think about the difference between remove, deland popin Python List and which one to use

remove: remove() removes the first matching value or object, not a specific indexing. lets say list.remove(value)