what's going on here?

John Salerno johnjsal at NOSPAMgmail.com
Thu Mar 16 17:31:38 CET 2006

This might be confusing to explain, because it's a question about an 
example in Beginning Python and I'll try to provide all the info I can.

First off, I'm reading a chapter on using the ReportLab modules to 
create a line graph from a set of data. The first implementation of the 
program uses a hard-coded list as the data source, the second 
implementation pulls the data from a URL. In either case, the data is of 
this format:

# year month predicted high low
2004	12	34.2	35.2	33.2
2005	01	31.5	34.5	28.5
(repeated many times)

In the first implementation, the data was a list of tuples, each tuple 
being one row of the data. So grabbing the data was done like this:

pred = [row[2]-40 for row in data]
high = [row[3]-40 for row in data]

We can safely ignore the '-40', that was just for positioning. So the 
variable for predicted would grab 34.2, 31.5, etc., high would get 35.2, 
etc. Easy enough.

Now, the second implementation does this:

for line in urlopen(URL).readlines():
	if not line.isspace() and not line[0] in COMMENT_CHARS:
		data.append(map(float, line.split()))

pred = [row[2] for row in data]
high = [row[3] for row in data]

(URL is the location of the data file online. The if statement just 
checks for blank lines and lines beginning with certain comment 
characters, so they can be ignored.)

So finally here's my question: If you are using data.append(), doesn't 
that just put all the numbers into one long list? How are the tuples 
still being created in this case so that the list comprehensions still 
work? It seems like there is no longer any 'row' to refer to in data.

The only thing I can think of is that each time through the for loop, a 
new item (tuple or list) is being created in the data list, so that each 
row of data really is being separated as its own element in the larger 
list, but that doesn't seem right.


More information about the Python-list mailing list