Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a list exists in Python

Tags:

python

What is the easiest way to check to see if a list or dict exists in python ?

Im using the following but this isn't working:

if len(list) == 0:
    print "Im not here"

Thanks,

like image 392
felix001 Avatar asked Jul 19 '12 07:07

felix001


People also ask

How do you check if a list exists or not in Python?

count() to check if the list contains. Another built-in method in Python, count() returns the number of times the passed element occurs in the list. If the element is not there in the list then the count() will return 0. If it returns a positive integer greater than 0, it means the list contains the element.

How do I check if a list exists?

Check if element exist in list using list.count(element) function returns the occurrence count of given element in the list. If its greater than 0, it means given element exists in list.

How do you check if a list exists in a list of lists Python?

To check if the item exists in the list, use Python “in operator”. For example, we can use the “in” operator with the if condition, and if the item exists in the list, then the condition returns True, and if not, then it returns False.

How do you check if a list is present in another list in Python?

Now, we can see how to Check if a list contain another list in Python. In this example, I have taken a variable as a list, and the if condition is used to check. If the check_list =[“orange”] is present in the list then it returns “List is present” else “List is not present”.


1 Answers

For the lists:

if a_list:
    print "I'm not here"

The same is for the dicts:

if a_dict:
    print "I'm not here"
like image 54
Igor Mandric Avatar answered Oct 21 '22 09:10

Igor Mandric