Change language

Python | Remove the given item from the list of lists

| |

Method # 1: Using List Comprehension

The logic behind this method is to reduce the size of the code and get the task done, using loops as a way of comprehending a list.

 ` # Python3 demo code ` ` # Removing an item from the list of lists ` ` # using comprehension list `   ` # initialization list ` ` test_list ` ` = ` ` [[` ` 4 ` `, ` ` 5 ` ` , ` ` 6 ` `], [` ` 5 ` `, ` ` 6 ` `, ` ` 4 ` `, ` ` 1 ` `], [` ` 4 ` `], [` ` 4 ` `, ` ` 8 ` `, ` ` 9 ` `, ` ` 10 ` `]] `   ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) `   ` # initialize the number to delete ` ` N ` ` = ` ` 4 ` ` `  ` # using the comprehension list ` ` # Removing an item from the list of lists ` ` res ` ` = ` ` [[ele ` ` for ` ` ele ` ` in ` ` sub ` ` if ` ` ele! ` ` = ` ` N] ` ` for ` ` sub ` ` in ` ` test_list] `   ` # print result ` ` print ` ` (` ` "The list after deletion of element:" ` ` + ` ` str ` ` (res)) `` `

Output:

` The original list: [ [4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]] The list after deletion of element: [[5, 6], [5, 6 , 1], [], [8, 9, 10]] `

Method 2: Using List Comprehension + List Slicing
In this method we we usually perform a task similar to the method described above. The difference is that we use list slicing for better readability.

 ` # Python3 demo code ` ` # Remove an item from the list of lists ` ` # use list comprehension + list slicing ` ` `  ` # init list ` ` test_list ` ` = ` ` [[` ` 4 ` `, ` ` 5 ` `, ` ` 6 ` `], [` ` 5 ` `, ` ` 6 ` `, ` ` 4 ` `, ` ` 1 ` `], [` ` 4 ` `], [` ` 4 ` `, ` ` 8 ` `, ` ` 9 ` `, ` ` 10 ` `]] `   ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) `   ` # initialize the number to delete ` ` N ` ` = ` ` 4 `   ` # using understanding list + list slicing ` ` # Remove an item from the list of lists ` ` for ` ` sub ` ` in ` ` test_list: ` ` sub [:] ` ` = ` ` [ele ` ` for ` ` ele ` ` in ` ` sub ` ` if ` ` ele! ` ` = ` ` N] `   ` # print result ` ` print ` ` (` `" The list after deletion of element: "` ` + ` ` str ` ` (test_list)) `

Exit :

` The original list: [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]] The list after deletion of element: [[5, 6], [5, 6, 1], [], [8, 9, 10]] `

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries