Python - Remove given element from list of lists


Introduction

Python's simplicity and readability have made it a versatile programming language. Python extensively supports lists and enables their manipulation, making it a powerful feature. A list is a common data structure in Python. The collection can have elements of any data type. Today we will explain how to delete a particular element from a list that contains lists in Python and examine two methods for deleting an item from a list of lists using Python. We will provide executable code examples and their outputs as well. Developers can conveniently customize their data structures by removing specific elements with ease. Programmers can manipulate complex nested lists and optimize their code for efficient data processing with confidence, having this knowledge.

Removing given element from list of lists

Definition

Python's list data structure is used to hold a collection of elements. The list can hold elements of various data kinds, such as numbers, texts, and even other lists. A list of lists, which is also called a 2D or nested list, comprises elements that are lists themselves. Let's now comprehend the definition of a list of lists. A list of lists is a collection of lists that are contained within a single main list as items. A loop must be used to travel over each individual list in order to delete the required element from each list within the list of lists. The aforementioned action will remove it from the nested list.

Syntax

list_name.remove(element)

Our objective is to eliminate an element from a list called list_name. Our aim is to eliminate an item from the given list. The identified element for removal has been specified.

Explanation of the syntax:

In Python, the developers use the remove () method, which is built-in and helps in removing the initial instance of a specific element from the list. The remove () function accepts a single argument.

Algorithm

  • Step 1: Iterate over each list in the list of lists.

  • Step 2: Check if the element is present in the current list.

  • Step 3: If the element is present, remove it from the list using the remove() method.

  • Step 4: If the element is not present, move to the next list.

  • Step 5: Steps 2-4 should be repeated until all of the lists in the list of lists have been checked.

Approach

  • Approach 1− Using a Nested Loop

  • Approach 2− Using list comprehension method.

Approach 1− Using a Nested Loop

Example

# Sample list of lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Element to remove
element = 5

# Iterate over each sublist
for sublist in matrix:
   if element in sublist:
      sublist.remove(element)

print(matrix)

Output

[[1, 2, 3], [4, 6], [7, 8, 9]]

Explanation of the approach 1:

To begin, the code defines a list of lists named'matrix' that has three sublists. Next, the code specifies an element that needs removal. It uses the `for` loop to iterate through every sublist in the `matrix`. The loop checks if the current sublist contains the desired element using the `in` keyword. The code calls the `remove()` method on the sublist if it finds the desired element to delete it. To confirm the removal, the code prints the updated `matrix`.

The program runs by iterating over the sublists, verifying and deleting the element when it is detected. The process of removing the element within a sublist takes place only once for its initial occurrence. The code displays the modified matrix after finishing the loop iterations. We confirm that the code has removed the specified element from the second sublist.

Approach 1 illustrates an uncomplicated technique for deleting a specified element from a list of lists through the utilization of a nested loop in Python.

Approach 2− Using list comprehension method.

Example

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
element_to_remove = 2

result = [[elem for elem in inner_list if elem != element_to_remove] for inner_list in list_of_lists]
print(result)

Output

[[1, 3], [4, 5, 6], [7, 8, 9]]

Explanation of the approach 2: Here in this example list_of_lists contain three sublists in it.The element_to_remove variable specifies that we should remove the number 2. List comprehension is utilized by the code, enabling a concise and efficient solution. It loops through every sublist in list_of_lists. It iterates over each element within the sublist in the nested comprehension. The conditional statement ensures that the new sublist includes only those elements which are not equal to the element_to_remove. The modified list, result, is obtained by removing the desired element from list_of_lists. The program prints the result, at last. The first sublist has successfully removed the element 2.

Conclusion

Concluding, Python's ability to be versatile and its extensive support for lists empower it as a language to manipulate intricate data structures. In Python, we have examined how to remove a particular element from a list of lists, also referred to as a 2D list or nested list.

The first approach utilized a nested loop to iterate over every sublist and remove the element when found. Loops and conditional statements were practically used to remove elements within nested structures in this approach.

The second approach used list comprehension which provided a more elegant and concise solution. We filtered out sublists that didn't contain the desired element by utilizing the power of list comprehension. We removed effectively in a single line of code.

Through detailed explanations of the code and output along with executable code examples. Our goal was to improve understanding of the discussed concepts. Readers were able to visualize the practical implementation and witness the expected results firsthand through these examples.

Programmers can effectively tackle such challenges with flexible solutions provided by Python's demonstrated approaches and simplicity.

To conclude, Python empowers users with robust tools and techniques to handle lists consisting of other lists. Depending on the situation choose the approach which better suits your solving condition of the given problem. Hope you got some worth after reading this article. Happy coding!

Updated on: 09-Oct-2023

457 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements