Question about tuple lengths

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed Dec 14 18:00:16 EST 2005


Dave Hansen wrote:

>>>> t2 = (1,2)  # 2 element tuple
>>>> t1 = (1,)   # 1 element tuple
>>>> t0 = (,)    # 0 element tuple?
>   File "<stdin>", line 1
>     t0 = (,)
>           ^
> SyntaxError: invalid syntax
>>>> t0 = ()     # Guess not, try parens with no comma
>>>> print t0, t1, t2
> () (1,) (1, 2)

That's still just a case of disambiguating. A single comma creates a
single-element tuple. Therefore, zero commas create a zero-length tuple.
Unfortunately, there's no way to indicate the lack of a comma without
disambiguating via parentheses.

Now, if you've got *more* than 1 element, you can omit the final comma.
*This* is the inconsistency (and it's the same with lists, dictionaries,
function argument lists, etc).

Tim Delaney



More information about the Python-list mailing list