unable to construct tuple with one item

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sun May 6 19:05:52 EDT 2007


On Sun, 06 May 2007 14:26:46 -0700, MRAB wrote:

> If it's the comma that makes the tuple, then wouldn't [1, 2] make a 2-
> tuple in a list, ie == [(1, 2)]? Hmm...


Yes, Python creates the tuple 1,2 and then the square brackets turn it
into a list.

I doubt the Python compiler is actually so inefficient that it constructs
an actual tuple before duplicating the data to create the list, but
conceptually you can think of it that way.

Likewise for {3:4, 5:9}.

What people mean is that the Python parser uses () for _grouping_ (with
the sole exception of an empty set of parentheses, which is special-cased
as an empty tuple. Commas separate items. If there is a pair of [ ]
surrounding the items, they are items of a list; if there is a pair of { }
surrounding them, they are items of a dictionary (and must be of the form
key:value); otherwise they are a tuple, _regardless_ of whether or not
there is a pair of ( ) around the group or not.



-- 
Steven.




More information about the Python-list mailing list