Is it possible to:
for k,v in kwargs.items()
if v == None or v == '' or v == 1.0 or v == False:
del kwargs[k]
without deleting the key if v == 0.0? (False seems to equal 0.0), and without deleting the keys who equal True.
Is it possible to:
for k,v in kwargs.items()
if v == None or v == '' or v == 1.0 or v == False:
del kwargs[k]
without deleting the key if v == 0.0? (False seems to equal 0.0), and without deleting the keys who equal True.
You should use v is False
instead of v == False
. The same applies for your comparison to None
. See PEP 8 - Style Guide for Python:
Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators.
Slow down guys with your advice, from PEP 8:
Don't compare boolean values to True or False using ==
Yes: if greeting: No: if greeting == True: Worse: if greeting is True:
Also comparing float value you should not use == but
abs(x-other) < verysmall
Or you can put it like this :
if v in (None, '', 1.0) or v is False:
Thanks for your replies. Using the suggestions, the problem was solved:
kwargs = {'None': None, 'empty': '', 'False': False, 'float': 1.0, 'True': True}
for k,v in kwargs.items():
if v in (None, '', 1.0) and v is not True:
del kwargs[k]
if v is False:
del kwargs[k]
kwargs
{'True': True}
-->
kwargs = {'None': None, 'empty': '', 'False': False, 'float': 0.0, 'True': True}
for k,v in kwargs.items():
if v in (None, '', 1.0) and v is not True:
del kwargs[k]
if v is False:
del kwargs[k]
kwargs
{'True': True, 'float': 0.0}