• Register
2 votes
18 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

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

1 vote
1 answer 26 views
26 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 samhaz 5k points
0 votes
1 answer 40 views
40 views
Problem: Hello sir, I was working on the list concept but having little confusion that how can I check if the list is a subset of another list in python Though this is an easy question still needed the clarity properly on this if possible answer this sir soon.
asked Jun 4 Gavin 15.3k points
1 vote
1 answer 36 views
36 views
Problem: I wrote a program to check if string matches regex list where : list = ["all*", "in*", "fo.*", "en"] string = "allinformen" The code I wrote is: import re # initializing list my_list = ["all*", "in*", "fo.*", "en"] # printing list print(" ... codition else it throws an error message: File "main.py", line 16 if res = True : ^ SyntaxError: invalid syntax What is the problem with my code?
asked Apr 28 samhaz 5k points
0 votes
1 answer 28 views
28 views
I wrote a code to check if a dictonary is empty or not. I wrote this code in python : def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False: socket.send("No ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) Does my isEmpty returns true and is there any problem in my code.
asked Mar 11 LizzyM 6.1k points
1 vote
1 answer 512 views
512 views
Problem : I am not completely expert in Python, but still I am willing to learn good practices while I develop my skills. I am trying to remove the high and low values from the list of numbers, I know how to do this , but I want to know the better way to do this. myrawlist = [ ... 2, 3, 4] mytrimmedlist = mylist[1:-1] #[1, 2, 3] I get the required output, but looking for the better way to do this.
asked Dec 14, 2019 alecxe 7.5k points