This article shows how to check if a list is empty, and how to create a empty list.
Contents
◆How to check if a list is empty
- Using "if not statement"
- Using "len( ) function"
- Comparison with empty list
◆How to create a empty list
- Using square brackets "[ ]"
- Using "list( ) function"
- [Supplement]How to create a empty 2D list
sponsored link
How to check if a list is empty
I show how to check if a list is empty.
The following three ways are explained below.
- Using "if not statement"
- Using "len( ) function"
- Comparison with empty list
Using "if not statement"
I show how to check if a list is empty by using "if not statement".
Converting an empty list to the boolean type returns False.
list_fruits = ["Apple", "Banana", "Orange"]
list_empty = []
print(bool(list_fruits))
# True
print(bool(list_empty))
# False
Use the above to check if a list is empty.
list_fruits = ["Apple", "Banana", "Orange"]
list_empty = []
if not list_fruits :
print("list is empty")
else :
print("list is not empty")
# list is not empty
if not list_empty :
print("list is empty")
else :
print("list is not empty")
# list is empty
This way is recommended in "PEP 8 -- Style Guide for Python Code".
・For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
# Correct:
if not seq:
if seq:# Wrong:
Reference : PEP 8 – Style Guide for Python Code
if len(seq):
if not len(seq):
Using "len( ) function"
I show how to check if a list is empty by using "len( ) function".
This is an example code.
list_fruits = ["Apple", "Banana", "Orange"]
list_empty = []
if len(list_fruits) == 0:
print("list is empty")
else :
print("list is not empty")
# list is not empty
if len(list_empty) == 0:
print("list is empty")
else :
print("list is not empty")
# list is empty
Comparison with empty list
I show how to check if a list is empty by using "len( ) function".
This is an example code.
list_fruits = ["Apple", "Banana", "Orange"]
list_empty = []
if list_fruits == []:
print("list is empty")
else :
print("list is not empty")
# list is not empty
if list_empty == []:
print("list is empty")
else :
print("list is not empty")
# list is empty
sponsored link
How to create a empty list
I show how to create a empty list.
Using square brackets "[ ]"
I show how to create a empty list by using square brackets "[ ]".
This is an example code.
list_empty = []
print(list_empty)
# []
print(len(list_empty))
# 0
Using "list( ) function"
I show how to create a empty list by using "list( ) function".
This is an example code.
list_empty = list()
print(list_empty)
# []
print(len(list_empty))
# 0
[Supplement]How to create a empty 2D list
As supplement, I show how to create a empty 2D list.
In following example code, an empty list is created by two different ways.
num = 5
empty_list1 = [[]] * num
empty_list2 = [[] for _ in range(num)]
print(empty_list1)
# [[], [], [], [], []]
print(empty_list2)
# [[], [], [], [], []]
Both are empty lists, but behave differently.
In following example code, an element is added to the empty list.
empty_list1[0].append(1)
empty_list2[0].append(1)
print(empty_list1)
# [[1], [1], [1], [1], [1]]
print(empty_list2)
# [[1], [], [], [], []]
This is due to the fact that the all empty lists generated by " [ [ ] ]* num " are objects with the same id.
print(id(empty_list1[0]))
# 2135914429192
print(id(empty_list1[1]))
# 2135914429192
print(id(empty_list2[0]))
# 2135913306504
print(id(empty_list2[1]))
# 2135898942984
When creating an empty 2D list, it is preferable that each list be independent.
Therefore, it is better to generate an empty 2D list by " [ [ ] for _ in range(num) ] ".
sponsored link