Skip to content Skip to sidebar Skip to footer

Loop To Append Multiple Lists To A Single List In Python

In the following code, row[8], row[9], ..., row[27] each contain many numbers. I need all of the numbers in row[8], row[9], etc. to append to stat_by_symbol as separate lists withi

Solution 1:

stat_by_symbol = dict((symbol, [[] for i in xrange(8,28)]) for symbol in symbols)
with open('zzdata.csv', 'rb') as f:
    reader = csv.reader(f)
    reader.next()
    for row in reader:
        for symbol, symbol_list in stat_by_symbol.iteritems():
            if symbol in row:
                for symbol_list2, cell in zip(symbol_list, row[8:28]):
                    symbol_list2.append(cell)

Post a Comment for "Loop To Append Multiple Lists To A Single List In Python"