![]() |
|
[Tut] How to Remove Empty Lists from a List of Lists in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Remove Empty Lists from a List of Lists in Python? (/thread-94852.html) |
[Tut] How to Remove Empty Lists from a List of Lists in Python? - xSicKxBot - 05-02-2020 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 Code: [x for x in list if x]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: https://trinket.io/embed/python/659a4282bd Method 1: List Comprehension How can you remove all empty lists from a list of lists? Say, you’ve got a list of lists Code: [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]and you want all empty lists removed to obtain the list of lists Code: [[1, 2, 3], [1, 2], [1, 2, 3, 4]]Solution: Use list comprehension Code: [x for x in list if x]Code: 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 Code: xCode: FalseCode: xCode: TrueYou can visualize the execution flow here by clicking the “Next” button: https://pythontutor.com/iframe-embed.html#code=lst%20%3D%20%5B%5B1,%202,%203%5D,%20%5B1,%202%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B1,%202,%203,%204%5D,%20%5B%5D,%20%5B%5D%5D%0Aprint%28%5Bx%20for%20x%20in%20lst%20if%20x%5D%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false Method 2: filter() An alternative is to use the Code: filter()Code: lst = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]
print(list(filter(lambda x: x, lst)))The filter() function takes two arguments:
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 Code: FalseCode: TrueRelated 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! |