Human
0
Q:

if list is not empty python

a = []

if not a:
  print("List is empty")
6
if len(li) == 0:
    print('the list is empty')
2
if '' in list:
  # do stuff
0
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Yes: if not seq:
     if seq:

No:  if len(seq):
     if not len(seq):
0
# An empty list object evaluates to False
a = []
not a
# Output:
# True
1
# The least verbose answer is:
# if a is a data structure, then
# "not a" will evaluate to True if a is empty.

# This is a more explicit example
# for a deque data structure (could be any other data structure)
dq = deque()
empty_dq = deque()

dq.append(1) # dq now contains 1 element

if dq == empty_dq:
  print("Empty.")
else:
  print("Not empty.")
0
>>> a = []
>>> not a
True
-1

New to Communities?

Join the community