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,
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.
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.
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.
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”.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With