Why tuple with one item is no tuple

Bill Mill bill.mill at gmail.com
Tue Mar 15 11:23:14 EST 2005


On Tue, 15 Mar 2005 16:16:34 GMT, Gregor Horvath <g.horvath at mx.at> wrote:
> Hi,
> 
>  >>>type(['1'])
> <type 'list'>
> 
>  >>>type(('1'))
> <type 'str'>
> 
> I wonder why ('1') is no tuple????

because, syntactically, those parens are for grouping, and do not
unambiguously define a tuple. It's a python gotcha. To define a
one-tuple, put a comma after the '1':

>>>type(('1',))
<type 'tuple'>

> 
> Because I have to treat this "special" case differently in my code.

you shouldn't have to; post your code if you still think you do.

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list