The most pythonic and the simplest way is to use the fact “boolean value of an empty object is False”
# Let's create lists
friends = ['Joey', 'Rachel', 'Chandler', 'Ross', 'Phoebe']
enemies = []
if not enemies:
print("List is empty")
List is empty
if friends:
print("List is not empty")
List is not empty
That's how we Check if a list is empty
That’s all for this mini tutorial. To sum it up, we learned how to Check if a list is empty.
Hope it was easy, cool and simple to follow. Now it’s on you.
Related Resources:
- Check if file or directory exists in Python Check if file or directory exists Use “path.exists” method of os library to check if file or directory exists for...
- List comprehension python | Create lists elegantly List comprehension List comprehension python explained with easy tutorial. Elegant way to create lists in python. # Given list ls...
- Create a list of files in a folder python Create a list of files in a folder python Create a list of files in a folder python | List all...
- Check if string contains substring python | ‘in’ keyword Check if string contains substring Use the ‘in’ keyword to check if string contains substring in python. # Let's creat...
- Learn Python easily with Detailed tutorials Python Learn python easily and efficiently with mini tutorials one snippet at a time. Python is widely used language for...
- Difference between append and extend in python Difference between append and extend Append adds an object to the end of a list whereas extend merges elements to...
- String Cases | Learn to use lower and upper case String Cases String method lower() converts string to all lower case. String method upper() converts string to all upper case. ...
- Concept of global variable in python Concept of Global Variable Declare global variable the same way we declare a variable. Use global keyword to change global...
- Dictionary comprehension python Dictionary comprehension Dictionary comprehension python explained with easy tutorial. Elegant way to create dictionaries in python. # Given list friends...
- Inline if/else (Ternary like) | Learn to use inline if/else Inline if else (Ternary like) conditions in Python Ternary like inline conditions are possible in python Below syntax is the...