• Register
2 votes
22 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 22 views
22 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
1 vote
1 answer 43 views
43 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 70 views
70 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, 2020 Gavin 15.3k points
1 vote
1 answer 38 views
38 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, 2020 samhaz 5k points
0 votes
1 answer 44 views
44 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, 2020 LizzyM 6.1k points
1 vote
1 answer 863 views
863 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
0 votes
1 answer 15 views
15 views
I am new to python programming.. I was trying to check if a string is empty or whitespace in a code : Mystr= "" Yourstr = " " print ("This string is empty : ", end = "") if(not Yourstr): print ("Yes") print ("This string is empty : ", end = "") if(not Mystr): print ("No") It’s output is : This string is empty : This string is empty : No Its not giving me actual result what can I do now?
asked Mar 16, 2020 LizzyM 6.1k points
1 vote
1 answer 43 views
43 views
Problem: I have a list that contains the values: list = [2, 4, 8, 16, 32, 2, 64, 128, 2, 256, 512, 2] I wrote a python program for this where I could find out how many times a value appears in a list python: def count_appearence(list, z): check = ... count_appearence(list, z))) NameError: name 'z' is not defined What is the problem with my code. Please help me solve this thing. Thanks in advance.
asked May 13, 2020 samhaz 5k points
1 vote
1 answer 43 views
43 views
Problem: I have 2 list that consist of some name and some numbers: list_1 = ['samhaz', 'porto', 'chris', 'evans'] list_2 = ['33', '14', '26', '30'] I tried to add this two list together(the names with the numbers). For that I wrote a python program: ... ' object has no attribute 'add' What is the problem in my program and how can I add those 2 list(names and numbers) together. Thanks in advance.
asked May 13, 2020 samhaz 5k points
1 vote
1 answer 27 views
27 views
Problem: I have a simple and basic level question to you guy. How can I replace an element in a list python? Yeah, it might be a silly question but need to be answered badly. I am struggling with this problem for the last thirty minutes almost. But I am unable to sort out this thing. Your kind help is much needed. Thanks
asked Apr 4, 2020 Gavin 15.3k points