Ankit
0
Q:

how to check if sometihng is empty python

my_str = ""
if not my_str:
  print("empty")
else:
  print("not empty")
#output: empty
4
# 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

New to Communities?

Join the community