List vs. Tuple

0 views
Skip to first unread message

Matthew Hirsch

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to
Hi All,

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')
>>> for x in list:
... print x
...
abcd
>>> for x in tuple:
... print x
...
a
b
c
d
>>>

Shouldn't the second loop also print 'abcd'?

Thanks for your help,
Matt


Tim Hochberg

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to

Matthew Hirsch wrote in message <37FCC0C4...@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

Charles G Waldman

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to Matthew Hirsch
Matthew Hirsch writes:

> 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')

Despite the name, "tuple" is not a tuple. It's a string. Parentheses
are (unfortunately, IMO) overloaded, being both tuple-delimiters and
the standard grouping operator, e.g. x=(1+3)*5

If you want to make a one-tuple, you need to use the following
slightly strange-looking construction:

tuple = ('abcd',)


Matthew Hirsch

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to
Thanks guys. It makes sense now.

Matthew Hirsch wrote:

> Hi All,


>
> 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')

Skip Montanaro

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to Matthew Hirsch

Matthew> Can someone explain to me why if there is only a string in a
Matthew> list/tuple they behave differently when looped through. For
Matthew> example,

>>>> list=['abcd']
>>>> tuple=('abcd')

Matthew,

Your tuple isn't a tuple at all. It's just a string. To indicate a
singleton tuple you must include a trailing comma:

tuple = ('abcd',)

This is an area of Python's syntax that often trips new users up (right
about when they get used to indentation-based block structure ;-). Since
parens are used both to force expression evaluation order and to define
tuples, the trailing comma in the singleton tuple disambiguates the two
cases. Lists don't have this problem since there is no ambiguity in the
semantic meaning of square brackets.

Now, stepping a bit away from the original problem...

You might find it helpful to get in the habit of always terminating tuples
and lists with a comma (though I must confess I only do this in one special
case myself). This makes it less likely you'll get bitten by the bug that
bit you. For large lists or tuples it can make it easier to insert new
entries. For example, suppose you had a static list of first names:

firsts = [ "Guido", "Jeremy", "Barry", "Mel" ]

Adding new names to the list gets to be a bother when your list gets big
enough cross a line boundary, so your natural instinct is to line wrap the
list:

firsts = [ "Guido", "Jeremy", "Barry", "Mel", "Adam", "John", "Dana",
"Chevy", "Eddie", "Gilda", "Jane", "Kevin" ]

After a few more additions you might find it difficult to see what's been
added and want them alphabetized. Once you cross this threshold, it's
easier to just make them one string per line and terminate each string with
a comma. You can then use an external sort program with impunity to sort
the list after adding new names:

firsts = [
"Adam",
"Barry",
"Chevy",
"Dana",
"Eddie",
"Fred",
"Gilda",
"Guido",
"Jane",
"Jeremy",
"John",
"Kevin",
"Mel",
]

Now, this is not a generally going to be a good way to maintain long static
lists or tuples, but for the occasional case it works, and the trailing
comma thing makes it easier to sort the items without worrying about where
you need to insert or delete commas.

Far enough afield for you?

Skip Montanaro | http://www.mojam.com/
sk...@mojam.com | http://www.musi-cal.com/
847-971-7098 | Python: Programming the way Guido indented...

Stidolph, David

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to Matthew Hirsch, pytho...@python.org
try

tuple = ('abcd',)

-----Original Message-----
From: Matthew Hirsch [mailto:me...@cornell.edu]
Sent: Thursday, October 07, 1999 10:48 AM
To: pytho...@python.org
Subject: List vs. Tuple


Hi All,

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')

>>> for x in list:
... print x
...
abcd
>>> for x in tuple:
... print x
...
a
b
c
d
>>>

Shouldn't the second loop also print 'abcd'?

Thanks for your help,
Matt


--
http://www.python.org/mailman/listinfo/python-list

Greg Ewing

unread,
Oct 9, 1999, 3:00:00 AM10/9/99
to
Charles G Waldman wrote:
>
> Parentheses
> are (unfortunately, IMO) overloaded, being both tuple-delimiters and
> the standard grouping operator

That statement is somewhat misleading -- parentheses actually
have NOTHING to do with tuple creation. You can do

t1 = 1, 2, 3
t2 = 4,

and get tuples. It's the commas, not the parentheses, which
make a tuple constructor. Keep that in mind and you won't get
confused as much.

You'll-still-get-confused-just-for-different-reasons,
Greg

Reply all
Reply to author
Forward
0 new messages