Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Remove Empty Lists from a List of Lists in Python?

#1
How to Remove Empty Lists from a List of Lists in Python?

Short answer: You can remove all empty lists from a list of lists by using the list comprehension statement [x for x in list if x] to filter the list.



In the following, you’ll learn about the two methods using list comprehension and the filter() function to remove all empty lists from a list of lists.

But before that, feel free to play with the code yourself:

Method 1: List Comprehension


How can you remove all empty lists from a list of lists? Say, you’ve got a list of lists

[[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]

and you want all empty lists removed to obtain the list of lists

[[1, 2, 3], [1, 2], [1, 2, 3, 4]].

Solution: Use list comprehension [x for x in list if x] to filter the list and remove all lists that are empty.

lst = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]
print([x for x in lst if x])
# [[1, 2, 3], [1, 2], [1, 2, 3, 4]]

The condition if x evaluates to False only if the list x is empty. In all other cases, it evaluates to True and the element is included in the new list.

You can visualize the execution flow here by clicking the “Next” button:

Method 2: filter()


An alternative is to use the filter() function to remove all empty lists from a list of lists:

lst = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]
print(list(filter(lambda x: x, lst)))

The filter() function takes two arguments:

  • the filter decision function to check for each element whether it should be included in the filtered iterable (it returns a Boolean value), and
  • the iterable to be filtered.

As filter decision function, you use the identity function that just passes the list through. Why does this work? Because only an empty list will be evaluated to False. All other lists will be evaluated to True (and, thus, pass the filtering test).

Related articles:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2020/05/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
Работа со свободным графиком. Нужны все
Last Post: Ronaldfloon
Today 04:59 AM
» Replies: 0
» Views: 1
Быстрое трудоустройство, высокая ЗП!
Last Post: Ronaldfloon
Today 02:32 AM
» Replies: 0
» Views: 8
Частная организация ищет работников по н...
Last Post: Ronaldfloon
Today 12:06 AM
» Replies: 0
» Views: 8
[Tut] Python __ne__ Magic Method
Last Post: xSicKxBot
Yesterday 01:27 PM
» Replies: 0
» Views: 28
(Indie Deal) Tales of Arise, Paradox, Ko...
Last Post: xSicKxBot
Yesterday 01:27 PM
» Replies: 0
» Views: 4
AppleInsider - These are the Mac feature...
Last Post: xSicKxBot
Yesterday 01:26 PM
» Replies: 0
» Views: 4
Microsoft - Microsoft Turing-NLRv5 achie...
Last Post: xSicKxBot
Yesterday 01:26 PM
» Replies: 0
» Views: 4
Xbox Wire - Crazy Athletics Is Available...
Last Post: xSicKxBot
Yesterday 01:26 PM
» Replies: 0
» Views: 4
News - CopperCube 6.5 Released
Last Post: xSicKxBot
Yesterday 01:26 PM
» Replies: 0
» Views: 4
Steam - Team Fortress 2 Update Released
Last Post: GilbertBib
Yesterday 08:34 AM
» Replies: 1
» Views: 131

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com



Discord Server © SickGaming.net 2012-2021