Difference in formatting list and tuple values

Sue Giller sag at hydrosphere.com
Thu Dec 13 13:26:37 EST 2001


For those who wonder why I use this construct, it's because it is a 
simple example taken from a more complicated one (taken from 
python module code - normalDate). I wanted to extend the formatting 
to accept a list as well as a tuple.

self.normalDate = int("%04d%02d%02d" % normalDate[:3])

where normalDate is expected to be at least a 3-item  tuple.  I was 
surprised when I extended my case from normalDate being a tuple 
to normalDate also being a list, and got an error.  

I just wanted to understand what was going on, cuz I'm somewhat 
new to python.  I knew that the list slice returned a list, and the tuple 
slice returned a tuple, and that the list was causing the problem but I 
couldn't figure out why.I had forgotten that % operator needs a tuple 
for its input. 

Thanks for the explanations.


On 13 Dec 01, at 17:25, Dr. Des Small wrote:

> sag at hydrosphere.com (Sue Giller) writes:
> 
> > I am trying to format values from either a list or a tuple, and I am
> > getting the following odd (to me) behavior.   
> 
> Well, you're doing an odd (to me) thing...





More information about the Python-list mailing list