Joe Blow wrote: > What is the best way to detect a TypeNone field in a tuple, or in a list? > > I am accessing a MySQL database using the MySQLdb Python interface... this > interface returns a tuple object type in response to SQL SELECT > statements. My understanding of the MySQLdb interface is that NULL > database values are returned as a Python 'None' object. > > Because I need to create some work fields based on the contents of the > database I am doing so by copying the tuple to a list object and then > calculating these work fields as needed. My problem is that I need to be > able to detect the Python 'None' objects and convert them to an integer > zero value field to enable my calculations to work. > > I'm new to Python so I'm sure I'm overlooking something simple – but what > is the easiest way to do a logical test for the existence of a TypeNone > field?
Since there is only one instance of TypeNone (the value we reference as None) the easiest test is if x is None: There is no need to create a list first: you can create a list as you iterate over the tuple: a = (1, 2, None, "a", "b") a = [0 if x is None else x for x in a] regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list