[Tutor] Dictionaries of Tuples of Strings

Luke Paireepinart rabidpoobear at gmail.com
Fri Feb 23 22:49:33 CET 2007


Terry Carroll wrote:
> On Fri, 23 Feb 2007, Luke Paireepinart wrote:
>
>   
>> That's because you can't make tuples of single values.
>> Think of it like this:
>> a TUple needs TWO elements or more.
>>     
>
> No, a tuple can also be of one element, or even none.  You can create a
> single-element tuple either directly through the use of a trailing comma,
> or via the tuple() method:
>
>   
>>>> t = "x",         # but this looks like a syntax error or a continued line
>>>> t = ("x",)       # I think this is a little clearer, but still ugly
>>>> t = tuple("x")   # my preference
>>>>         
>
>   
 >>> x = tuple("hello")
 >>> x
('h', 'e', 'l', 'l', 'o')

It apparently doesn't work that way with iterables.
I think the 'way you'd expect it to work' using your preference is to create
('hello')
but if you're doing intergers or single-character strings, it shouldn't 
matter either way.



More information about the Tutor mailing list