Using reverse iteration to clean up a list

Michael Hoffman cam.ac.uk at mh391.invalid
Sun Mar 13 10:36:05 EST 2005


tkpmep at hotmail.com wrote:
> Thank you all so much for the generous dollop of help: the dictionary
> suggestion is particularly helpful. The problem arises as follows: A
> software application stores the securities held in a portfolio in a
> .csv file, one row per security, with three colulmns.
> 
> The first has a security identifier or descriptor (such as a ticker)
> the second has a single letter that identifies the type of the
> identifier (T for ticker, C for cusip etc.) and the third has the
> number of shares. A typical line looks like this:
> 
> IBM, T, 500
> 
> I need to read in one or more portfolios and aggregate their holdings.
> To do so, I read in the portfolios using the csv package, convert each
> line to a list and then append it to a list of lists. Eventually the
> list of lists contains all the securities, and can then be sorted and
> aggregated.
> 
> I suppose I could convert it instead to a dictionary, and the most
> natural key would be the first two items, i.e. a portfolio containing
> just 500 shares of IBM ought to be represented as
> {("IBM", "T") : 500 }
> 
> How can I translate the data I read in using csv.reader into a
> dictionary?

portfolio = {}

for row in csv.reader(infile):
     key = tuple(row[:2])

     portfolio[key] = portfolio.get(key, 0) + int(row[2])

You could also do a groupby solution with
itemgetter(slice(0, 2))--thanks to Steven Bethard for recently
pointing out the possibility here of doing that. I'd go with the
dict for this application though.
-- 
Michael Hoffman



More information about the Python-list mailing list