Delete all element in list python

How to remove all elements from a list in Python

Here in this tutorial, we are going to learn how to remove all elements from a list in Python. We can think of doing this in many ways. But here in this tutorial you will learn how to remove elements from a list using clear() and del().

Let us now start our tutorial.

Code to Remove all elements from a list in Python

Now, let us see how del() keyword is used to remove all elements from a list in Python.

del()

This method deletes all the elements in the range given. This del keyword deletes variables, lists, or parts of list, etc., Other similar keywords like pop(), remove() deletes elements in a particular range given but not all elements in a list.

Читайте также:  Login & Registration Form

The del() removes all elements from the list in Python. Let us see this through code now.

Now, let us see the usage of del() in another example also.

clear()

Now, let us see how to use clear() to remove all elements from a list.

The clear() method deletes all elements in a list. It doesn’t take any parameters. It doesn’t return any value also. This clear() deletes all elements from the list, set, dictionary etc., This is the easy way of deleting elements in Python.

Now, let us see this using example.

Let us see this through another example.

a=[2,6,5,'u','7'] print(a.clear()) print(a)

So, we have seen how to delete elements in a list in two ways in this tutorial.

Источник

Delete All Elements Of A List In Python

In python, we use lists in almost every program. We have already discussed ways to compare two lists and to reverse a list in python. In this article, we will discuss different ways to delete all the elements of a list in python.

Delete All Elements Of A List In Python Using The clear() Method

The pop() method is used to delete the last element of a list. When invoked on a list, the pop() method returns the last element of the list and deletes it from the list. We can use the while loop and the pop() method to remove all the elements of the list.

For this, we can keep invoking the pop() method on the list inside the while loop until the list becomes empty. Once the list becomes empty, the while loop will stop its execution and we will get a list that has no elements. You can observe this in the following program.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] print("The original list is:") print(myList) # deleting elements using the pop() method while myList: myList.pop() print("List after deleting all the elements:",myList)
The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9] List after deleting all the elements: []

Instead of using the pop() method several times, we can use the clear() method to delete all the elements from a list. The clear() method, when invoked on a list, deletes all the elements of the list.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] print("The original list is:") print(myList) # deleting elements using the clear() method myList.clear() print("List after deleting all the elements:",myList)
The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9] List after deleting all the elements: []

Delete All Elements Of A List In Python Using The del Statement

The del statement is used to delete an object in python. We can also use the del statement to remove the elements of a list. For this, we will create a slice of the list that contains all the elements. After that, we will delete the slice. As the slice contains references to the elements in the original list, all the elements in the original list will be deleted and we will get an empty list. You can do it as follows.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] print("The original list is:") print(myList) # deleting elements using the del method del myList[:] print("List after deleting all the elements:", myList)
The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9] List after deleting all the elements: []

Delete All Elements Of A List In Python Using The * Operator

This is one of the least used ways to remove elements from a list in Python. You might be knowing that multiplying a list to any number N repeats the elements of the list N times. In the same way, when we multiply a list to 0, all the elements of the list are deleted. So, we can multiply the given list with 0 to remove all of its elements as follows.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] print("The original list is:") print(myList) # deleting elements using * myList = myList * 0 print("List after deleting all the elements:", myList) 
The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9] List after deleting all the elements: []

Conclusion

In this article, we have discussed four different ways to delete all the elements of a list in python. To know more about lists in python, you can read this article on list comprehension. You might also like this article on how to get the last element of a list in python.

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Источник

How to remove elements from a list in Python?

Remove Elements Python List Featured Image

In this article, we will go through all the methods to remove elements from a list in Python.

Python lists are the most basic data structure used in day-to-day programming. We come across situations where we need to remove elements from lists and in this article, we’ll discuss exactly that.

1. Remove Elements From a List Based on the Values

One of the reasons Python is a renowned programming language is the presence of the numerous inbuilt functions. These inbuilt functions are very handy and thereby make Python very convenient to write.

remove() function

Python has an inbuilt function, remove() that helps us to remove elements based on the value.

# List of integers lis = [3, 1, 4, 1, 5, 9, 2, 6, 5] # Remove element with value = 1 lis.remove(1) # Printing the list print(lis) # Remove element with value = 9 lis.remove(9) # Printing the list print(lis)
[3, 4, 1, 5, 9, 2, 6, 5] [3, 4, 1, 5, 2, 6, 5]

The key things to note here are:

  • The remove() function takes in one argument — the value to be removed.
  • If there are multiple occurrences of the given value, the first one is removed.
  • Removing an element does not leave a blank space at that position, it just shifts the following elements to the left.
  • In case, there is no such element in the list, then the script raises an error.

Error-free usage of remove() function

There is an easy way to bypass the error while removing an element in case the programmer is unaware of its presence on the list. We’ll do this using the if condition.

# List of integers lis = [1, 4, 2, 6, 1, 9, 10] # Value to be removed val = 6 # Check if the list contains the value if val in lis: # Remove the value from the list lis.remove(val) # Printing the list print(lis)

In the above code snippet, we first check the presence of the value in the list before removing.

Remove all the occurrences of a value in a list

As we previously mentioned, remove() function only removes the first occurrence of a value. In order to take out all the instances of said value, we will do use the while loop.

# List of integers lis = [1, 4, 2, 6, 1, 9, 10] # Value to be removed val = 1 # Run until the list containes the value while val in lis: # Remove the value from the list lis.remove(val) # Printing the list print(lis)

This sums up the usage of remove() function.

2. Removing elements based on an index

There can be a few ways to remove elements based on the index. Let us quickly go through each one of them.

del keyword

del is a powerful tool in Python which is used to remove entire objects. It can also be used to remove elements from a given list.

# List of integers lis = [3, 1, 4, 1, 5, 9, 2, 6, 5] # Removing element from the start (index = 0) del lis[0] # Printing the list print(lis) # Removing element from the last (index = -1) del lis[-1] # Printing the list print(lis)
[1, 4, 1, 5, 9, 2, 6, 5] [1, 4, 1, 5, 9, 2, 6]

Some of the observations derived from the above script are:

  • del is not a method. It is a statement that deletes the item placed after it.
  • Removing an element from a specific index shifts the next value to that specific index if it is not the last index.
  • Providing an index more than (or equal to) the length of the list will raise an “out of range” error.

pop() function

As the name suggests, pop() function pops out an element from a specified index.

# List of integers lis = [3, 1, 4, 1, 5, 9, 2, 6, 5] # Removing the fourth element (index = 3) lis.pop(3) # Printing the list print(lis) # Removing the second last element (index = -2) lis.pop(-2) # Printing the list print(lis)
[3, 1, 4, 5, 9, 2, 6, 5] [3, 1, 4, 5, 9, 2, 5]

What we learnt about pop() method here is:

  • It takes a single argument — the index of a list
  • It removes the element from the list based on the given index. The following elements shift to the left.
  • It supports backward indexing.
  • It will raise an “out of range” error if the index is not present for the list.

We have a complete article on the use of pop() method.

3. Removing a range of elements from a list

Python has a provision of removing a range of elements from a list. This can be done by del statement.

# List of integers lis = [3, 1, 4, 1, 5, 9, 2, 6, 5] # Removing first and second element del lis[0:2] # Printing the list print(lis) # Removing last two elements del lis[-2:] # Printing the list print(lis)

Let us try to understand the process:

  • To remove multiple elements from a list in a sequence, we need to provide a range of elements to the del statement.
  • A range of elements take a starting index and/or an ending index, separated by a colon ‘:’ .
  • The values to be deleted include starting index, but not the value at the ending index.
  • In case, the ending index is missing, the range includes all the elements till the end of the list.

4. Remove all the elements from the list

Python provides a method to empty the entire list in a single line.

# List of integers lis = [3, 1, 4, 1, 5, 9, 2, 6, 5] # Removing all elements lis.clear() # Printing the list print(lis)

If the function applied to an empty list, it does not raise any error.

Conclusion

It is up to your discretion to use the way of removing elements from a list, either by value or index. Different circumstances require a different approach, therefore Python provides various methods of removing elements from a Python list.

We hope the reader had no difficulty in following the article. Feel free to comment below for any queries related to the subject.

Источник

Оцените статью