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