Python Program to Remove a Subset from a List


Python is an interpreted, object–oriented, high level, programming language with dynamic semantics. Developed by Gudio Van Rossum in 1991. It supports multiple programming paradigms, including structured, object-oriented and functional programming.

What is a List in Python

List is a data structure in python, which is changeable, mutable and iterable ordered sequence of element. They are used to store multiple elements in a single variable. List allows duplicate elements.

Lists are mutable which means that you can change the values of list, we can add an element, remove, repeat and allows indexing and slicing like strings an element from a list by using different operators.

  • Unlike array, which can contain objects of a single type, a list can contain a mixture of object.

  • This makes the list data structured one of the most powerful and flexible tools in python programming.

  • List elements can be accessed using index. The index is position of element in the list.

Example

List can be created by using square brackets. By placing elements inside square bracket[],separated by commas. For example: list1= [“a”,”b”]. There is a one more type of list known as “Nested list”. It is list within list.

list1=[“a”,”b”[1,2]”c”]

In this article we will know about how to remove a subset from a list by using different functions. There are many function to remove subset from a list like remove(),pop(),del(),clear().

How to Remove a Subset From List

Here, we have given a list of numbers and we have to remove a subset from a list by using different methods.

To remove a subset from list in python, there are four different methods.

  • The first is by using the remove() method which searches for a specified value and removes it from the list if found.

  • Secondly, pop() can be used to remove an element at a given index or to delete the last item of the list.

  • Thirdly, del() is used when you want to delete items based on their indices instead of values.

  • Lastly, clear() can be used when you need to empty out all elements in a list without deleting it entirely.

By Using Remove()

The remove() method is one of the methods used to delete a subset from a list in Python. This function removes an element from the list based on its value, rather than its index number.

When a list contains duplicate elements, the remove() function can be used to delete that element from the list. If the specified element is not present in the list, an error will be thrown stating that no such element exists in the list.

The remove() method does not return any value; it requires a value as its argument and this must be of an appropriate data type.

Example

We are using remove() method in the example given below to remove number “78” from the list ‘I’.

l=[12,78,89,78,32]
l.remove(78)
print("the output is:",l)

Output

the output is: [12, 89, 78, 32]

Example

In the following example, we are using remove() method to remove the elements from the ‘1’ list.

l=["kunnal","aman","mohan","messy","aman","shiny"]
l.remove("aman")
print("the output is:",l)

Output

On executing the above program the following output is generated

the output is: ['kunnal', 'mohan', 'messy', 'aman', 'shiny']

By Using pop()

The `pop()` method is used to remove an item from a list, a complex data type. When invoked, the item located at the specified index will be removed and returned as the outcome of this method.

Example

The following example shows how to remove the fourth item in the list (in this case, "messy") by using pop() method and prints out a new list without it.

l=["kunnal","aman","mohan","messy","rahul","shiny"]
l.pop(3)
print("the output is:",l)

Output

the output is: ['kunnal', 'aman', 'mohan', 'rahul', 'shiny']

Example

The following example uses pop() method to remove unwanted values, and we have given index 3 as follows -

l=[12,24,36,48,60,72,84]
l.pop(3)
print("the output is:",l)

Output

the output is: [12, 24, 36, 60, 72, 84]

Using del

The del operator can be used to remove items or elements at a specified index location from a list. It is important to note that the removed item is not returned.

Additionally, slices of a list can also be deleted using this operator. However, it should be noted that tuples and strings cannot have their individual items deleted as they are immutable objects - meaning they cannot be changed after creation. Nevertheless, entire tuples and strings may still be deleted with the use of del.

Example

In the following example, we are using the del() operator to remove the elements at the specified index location from the list. As we can see, we have given index [1:5]

list=[12,24,36,48,60,72,84]
del list[1:5]
print("the output is:",list)

Output

the output is: [12, 72, 84]

Example

The following example involves a list of strings with 7 elements. The del command is used to delete elements from the list, and in this case it has been used to delete elements at index 1 (which is "govind") up until index 5 (which is "anu"), leaving only 3 elements in the list: "kunnal", "riya" and "alisa". Therefore, when this code is run, the output will be ["kunnal", "riya", "alisa"].

list=["kunnal","govind","chitrang","anu","raag","riya","alisa"]
del list[1:5]
print("the output is :",list)

Output

the output is : ['kunnal', 'riya', 'alisa']

By Using clear()

The clear() is a built-in function in Python that allows the user to remove all items from a list. It takes no parameters and returns an empty list (or other mutable data types like dictionaries).

This can be useful when you want to reset or reinitialize a list, set, or dictionary for reuse. You can also use it to clear out any previous values stored in the data structure before adding new ones.

Using clear() helps keep your code clean and organized since you don't have to loop through every item in the data structure just to delete them one by one.

Example

The example given is a list containing seven elements. We, then uses the clear() method to remove all of the items in the list, leaving it empty.

list=["kunnal","govind","chitrang","anu","raag","riya","alisa"]
list.clear()
print("the output is:",list)

Output

the output is: []

Example

The following example is clearing a list. The original list contains the numbers 12, 45, 67, 89 and 79. We are using the clear() method to clear the list.

list=[12,45,67,89,79]
list.clear()
print("the output is:",list)

Output

the output is: []

Conclusion

In this article, we have briefly discussed about the different methods used to remove a subset from a list.

Updated on: 21-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements