• Register
2 votes
33 views

Problem:

Let’s say I have a list x = [] in python. How could I check this list is empty or not?

Thanks in advance.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

There are several ways to check a list is empty or not in python. The most used and easy way is:

>>> x=[]
>>> if not x:
    print ("This list is empty")
else:
    print ("Yeah! you got it")

Another way is to use the if len(list)

def Enquiry(x): 
    if len(x) == 0: 
        return 0
    else: 
        return 1
          
# Driver Code 
x = [] 
if Enquiry(x): 
    print ("Yeah! you got it") 
else: 
    print("This List is empty") 

I hope things are clear to you now. Keep digging.

13 9 6
94,240 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: How I can solve this issue : check if a list is empty python
asked Mar 15 Wafa Abu Yousef 6.1k points
0 votes
1 answer 11 views
11 views
Problem: HI KODLOGS: I want to understand how to check if list is eampty in python please explain in detail with examples. For example, if passed the following: a = [] How do I check to see if a is empty?
asked Jan 26 Mashhoodch 13k points
0 votes
1 answer 24 views
24 views
Problem: I am a new student in learning python and i am not sure how to check if a list is empty in python. For example, if the following is passed: a = [] How to check if it is empty?
asked Nov 5, 2020 Han Li 710 points
0 votes
1 answer 11 views
11 views
Problem: How to I Solve this: python check if list is not empty HELP! I need help understanding this: python check if list is not empty Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 3 Mashhoodch 13k points
0 votes
0 answers 2 views
2 views
Problem: For example, if the following is approved: a = [] How can I check if it ais empty?
asked Mar 17 sasha 16.2k points
0 votes
1 answer 2 views
2 views
Problem: How to check if a number already exist in a list in python?
asked 1 day ago Humaira ahmed 15.1k points
0 votes
1 answer 6 views
6 views
Problem: How to I Solve this: check if something is in a list python HELP! I need help understanding this: check if something is in a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 6 Mashhoodch 13k points
1 vote
1 answer 91 views
91 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16, 2020 samhaz 5k points
0 votes
1 answer 2 views
2 views
Problem: Python check if element is in list.
asked 4 days ago Dan phillip 4.8k points
0 votes
1 answer 4 views
4 views
Problem: how to I Solve this: python check if string is in list HELP! I need help understanding this: python check if string is in list Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 9 Mashhoodch 13k points