Beginner question: Converting Single-Element tuples to list

Jeff Epler jepler at unpythonic.net
Mon Jun 27 18:11:33 CEST 2005


On Mon, Jun 27, 2005 at 08:21:41AM -0600, John Roth wrote:
> Unfortunately, I've seen that behavior a number of times:
> no output is None, one output is the object, more than one
> is a list of objects. That forces you to have checks for None
> and list types all over the place.

maybe you can at least push this into a single convenience function...

def destupid(x, constructor=tuple, sequencetypes=(tuple, list)):
    if x is None: return constructor()
    if isinstance(x, sequencetypes): return x
    return constructor((x,))

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050627/f9e0df4c/attachment.sig>


More information about the Python-list mailing list