Difference in formatting list and tuple values
Sue Giller
sag at hydrosphere.com
Thu Dec 13 12:00:56 EST 2001
I am trying to format values from either a list or a tuple, and I am
getting the following odd (to me) behavior. I can format the tuple
entry using %d, but I get an error when I use the same formatting
for the same data in a list.
Why does this happen?
>>> l = [1,2,3] # list
>>> t = (1,2,3) # tuple
>>> print "%02d" % t[:1] # format first entry in tuple ok
01
>>> print "%02d" % l[:1] # get error with list
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: an integer is required
>>> print "%02d" % tuple(t[:1]) # cast to a tuple works ok
01
More information about the Python-list
mailing list