List vs. Tuple

Tim Hochberg hochberg at psn.net
Thu Oct 7 10:58:33 EDT 1999


Matthew Hirsch wrote in message <37FCC0C4.DF34352F at cornell.edu>...
>Can someone explain to me why if there is only a string in a list/tuple
>they behave differently when looped through.  For example,
>
>>>> list=['abcd']
>>>> tuple=('abcd')
[SNIP]

The cause of your confusion is that 'tuple' is not a tuple, instead it is
just a parenthesized expression. In order to differentiate tuples from
parenthesized expressions a trailing comma is required in the singleton
tuple case.

('abcd',) # is a tuple while
('abcd') # is not

It might be clearer if you consider the following:

notATuple = (2+3) # is the scalar 5
isATuple = (2+3,) # is the tuple (5,)

Hope that helps clear things up,

-tim






More information about the Python-list mailing list