How can I have one element list or tuple?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Nov 1 12:29:27 EDT 2007


On Thu, 01 Nov 2007 09:21:40 -0700, nico wrote:

> The following example returns a string type, but I need a tuple...
>>>> var = ("Hello")
>>>> print type(var)
> <type 'str'>
> 
> I need that for a method parameter.
> Thx


It is the comma, not the brackets, that create tuples. The brackets are 
recommended for clarity, but aren't always required (except for grouping).

>>> 1, 2, 3
(1, 2, 3)

The only exception is the special case of an empty tuple, which you 
create with an empty pair of brackets:

>>> ()

So for a one-element tuple:

>>> "Parrot",
('Parrot',)



-- 
Steven.



More information about the Python-list mailing list