• Home
  • Tutorials
  • Python Runner
  • Other
    • Become a Teacher
    • Profile
PythonBabuPythonBabu
  • Home
  • Tutorials
  • Python Runner
  • Other
    • Become a Teacher
    • Profile

Fundamental

  • Home
  • Fundamental
  • Difference between remove, del, pop and clear in python list

Difference between remove, del, pop and clear in python list

  • Posted by Python Babu
  • Date December 21, 2019
  • Comments 0 comment

Introduction

In this article, we will learn different methods to delete items from the python list. So there are 4 methods by which we can delete or remove elements from the python list. which are as follows

  1. remove
  2. pop
  3. clear
  4. del

Following figure shows brief difference

Difference between remove, del, pop and clear in python list

Now let’s understand one by one

remove()

The remove method removes the first matching element. remove() takes exactly one argument which will be deleted from the list

Example

list = [9, 8, 7, 8, 6, 5]
list.remove(8) #This method remove first matching 8 in this list

Output

remove() method

Note – If the element doesn’t exists in the list, it throws valueError exception, example shown in the following figure

When value not found in the list

What remove() method returns?
remove() method does return any value or returns None


pop()

The pop method, removes the last value from the list or returns that removed value. Syntax of the pop() method is as follows

Syntax

<list>.pop([index])
  • index is optional parameter in pop() method
  • If index given then, removes the item at a specific index and returns removed value.
  • If index is not available then, the last element is popped out and returns that.

Example

>>> list = [9, 8, 7, 8, 6, 5]
>>> list.pop()
5
>>> list
[9, 8, 7, 8, 6]
>>> list.pop(2)
7
>>> list
[9, 8, 8, 6]

Note – If index doesn’t exists then it throws indexError exception, example shown in the following figure

pop method

clear()

The clear() method , removes all elements of the list and returns None.

Example

>>> list = [9, 8, 7, 8, 6, 5]
>>> list
[9, 8, 7, 8, 6, 5]
>>> list.clear()
>>> list
[]

del

The del, Remove items by index or slice. This is one of the dangerous method.

There are two different syntax for del method: [] and ().

Example of the delete method is as follows

Example 1: With index

>>> list = [9, 8, 7, 8, 6, 5]
>>> del list[2]
>>> list
[9, 8, 8, 6, 5]
>>> del list[-1]
>>> list
[9, 8, 8, 6]

Example 2: With slice

>>> list = [9, 8, 7, 8, 6, 5]
>>> del list[2:]
>>> list
[9, 8]
>>> list = [9, 8, 7, 8, 6, 5]
>>> del list[2:4]
>>> list
[9, 8, 6, 5]

Example 3: Slice with steps. You can also specify step as [start:stop:step]

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> del list[0:9:2]
>>> list
[2, 4, 6, 8, 10]

Example 4: With parentheses

>>> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> del(list)
>>> list
<class 'list'>

Tag:clear, del, pop, python

  • Share:
author avatar
Python Babu
Sourabh Somani is 4 times Microsoft MVP and 7-times C# Corner MVP, is a tech lead, contributor, International Speaker, Author, and the founder of www.pythonbabu.com. After possessing a B.Tech degree in Computer Science, he found his niche in core programming and database development. His core competencies include Mobile and web applications development using ASP.NET, Python, MVC, Node.js, C#, Java Script, jQuery, SQL Server, NoSQL, MongoDB, and Angular. With his extensive experience of several years, passion toward learning new technologies, and a zeal to give back to the developer community, Sourabh contributes his knowledge via very helpful articles and blogs on various community sites.

Previous post

Conditional Expression in Python
December 21, 2019

Next post

How to check list is empty or not?
January 12, 2020

You may also like

How to check list is empty or not?
12 January, 2020

Introduction In this blog, you will know that, how can we check whether the list is empty or not in python. Suppose you have a list like as follows As …

Conditional Expression in Python
15 December, 2019

Introduction In Python 2.5, Guido van Rossum added conditional expression. In Python there is no ternary conditional operator but by using Conditional Expression we can work like a Conditional Operator …

Leave A Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to check list is empty or not?
  • Difference between remove, del, pop and clear in python list
  • Conditional Expression in Python
  • Find all the Armstrong Numbers in a Range using python
  • Find Computer IP Address using Python

Recent Courses

Machine Learning with TensorFlow

Machine Learning with TensorFlow

Hello Friends, In this tutorial you will learn step by...
3 lessons
Python Tips & Tricks

Python Tips & Tricks

In this Course you will know different different Python Tips...
10 lessons
Python Tutorial for Beginners

Python Tutorial for Beginners

In this tutorial series you will get complete python beginner...
39 lessons
Go To All Recent Tutorials

Tags

armstrong number clear Conditional Expression del empty expression expressions ip ip address list pop python range

Copyright © Python Babu