Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Dict value is null

In [54]: User
Out[54]: {0: {'uid': ' rpatil\n'}, 1: {}}

In [55]: User[0]
Out[55]: {'uid': ' rpatil\n'}

In [56]: User[1]
Out[56]: {}

In [57]: if User[1] == '':
   ....:     print 'Null'
   ....:

In [58]:

So how to check Value is Null in multi dict

I've Tried Following Links but not work None value in python dictionary

like image 478
Rahul Patil Avatar asked Sep 23 '13 16:09

Rahul Patil


People also ask

Can dictionary values be null?

In C#, Dictionary is a generic collection which is generally used to store key/value pairs. In Dictionary, the key cannot be null, but value can be.

How do I check if a dictionary is null in Python?

Check if a Python Dictionary is Empty by Checking Its Lengthif len(empty_dict) == 0: print('This dictionary is empty! ')

How do you check if a value is in the dictionary None?

Method #1 : Using all() + not operator + values() The combination of above functions can be used to perform this particular task. In this, we check for all the values using all function extracted using values function. The not operator is used to inverse the result to check for any of None value.

How do you check if a dictionary key has a value?

To check if a value exists in a dictionary, i.e., if a dictionary has/contains a value, use the in operator and the values() method. Use not in to check if a value does not exist in a dictionary.


1 Answers

Empty dictionaries are falsey. Just use if not dic.

>>> testDict = {}
>>> if not testDict:
        print "Empty"


Empty

So, in your code.

>>> User = {0: {'uid': ' rpatil\n'}, 1: {}}
>>> if not User[1]:
        print "NULL"


NULL
like image 87
Sukrit Kalra Avatar answered Oct 02 '22 20:10

Sukrit Kalra