Create Lists and Tuples inside a Nested Dictionary in Python 3

Question:

I’m trying to read a text file into a nested dictionary which further goes onto a tuple within a list.
The contents of the file look like this:

1976,A,001,58906,98257,002,66288,90069,003,106935,0,004,141490,34531,005,113553,0,006,69384,92113,007,110496,0
1976,B,000,34194,83722
1976,C,001,68404,96397,002,106054,71765,003,88854,79162,004,92435,93154
1976,D,001,116217,52565,002,144780,22819,003,0,1,004,1,0
1976,E,001,160477,56539,002,88829,121290,003,139779,52075,004,75844,75193,005,103746,64008,006,86493,35359,007,147064,45863,008,122342,68374,009,116398,44607,010,111992,38088,011,107618,62435,012,61526,130332,013,135291,63130,014,123285,46674,015,92735,35700,016,104545,91160,017,103898,54270,018,56683,101658,019,68722,124201,020,71193,146158,021,101837,44094,022,68543,114769,023,130619,86434,024,108296,51478,025,57966,17737,026,59093,112619,027,94988,114623,028,114612,28303,029,82515,10852,030,82767,28503,031,83155,0,032,92034,35394,033,77807,95398,034,100988,98147,035,87472,76765,036,90830,49368,037,49021,133634,038,103317,59092,039,86745,122657,040,102132,148512,041,94590,128784,042,103062,32565,043,93475,173576
.... and so, on..

I had to achieve an output like this:

{
    '1976': {'A': [VALUES("001", 58906, 98257),
                         VALUES("002", 66288, 90069),
                         VALUES("003", 106935, 0),
                         VALUES("004", 141490, 34531),
                        VALUES("005", 113553, 0),
                         VALUES("006", 69384, 92113),
                         VALUES("007", 110496, 0)],
            'B':   [VALUES("000", 34194, 83722)],
            'C':  [VALUES("001", 68404, 96397),
                         VALUES("002", 106054, 71765),
                         VALUES("003", 88854, 79162),
                         VALUES("004", 92435, 93154)],
            ...etc 
        }

The code that I have currently written is:

import csv
results = {}
with open("file.txt", 'r') as f:
    for line in f:
        list = line.split(",")
        year= str.strip(list[0])
        letter=str.strip(list[1])
        for x in range(2, (len(list))):
            value= str.strip(list[x])
            if year not in results:
                results.update({year: {letter: value}},)
            elif state not in results[year].keys():
                results[year].update({letter:value}) 
            else:
                results[year][letter]=[].append(value)
print(results)

The output which I get is:

{'1976': {'A': None, 'B': None, 'C': None...} and so on

I can’t figure out why the lists are giving me none. If I try this : results[year].update({letter:(value})

Asked By: degeneratematter

||

Answers:

Expected and Achieved Output:

{'1976': {'A': ['001', '58906', '98257', '002', '66288', '90069', '003', '106935', '0', '004', '141490', '34531', '005', '113553', '0', '006', '69384', '92113', '007', '110496', '0 1976', 'B', '000', '34194', '83722 1976', 'C', '001', '68404', '96397', '002', '106054', '71765', '003', '88854', '79162', '004', '92435', '93154 1976', 'D', '001', '116217', '52565', '002', '144780', '22819', '003', '0', '1', '004', '1', '0 1976', 'E', '001', '160477', '56539', '002', '88829', '121290', '003', '139779', '52075', '004', '75844', '75193', '005', '103746', '64008', '006', '86493', '35359', '007', '147064', '45863', '008', '122342', '68374', '009', '116398', '44607', '010', '111992', '38088', '011', '107618', '62435', '012', '61526', '130332', '013', '135291', '63130', '014', '123285', '46674', '015', '92735', '35700', '016', '104545', '91160', '017', '103898', '54270', '018', '56683', '101658', '019', '68722', '124201', '020', '71193', '146158', '021', '101837', '44094', '022', '68543', '114769', '023', '130619', '86434', '024', '108296', '51478', '025', '57966', '17737', '026', '59093', '112619', '027', '94988', '114623', '028', '114612', '28303', '029', '82515', '10852', '030', '82767', '28503', '031', '83155', '0', '032', '92034', '35394', '033', '77807', '95398', '034', '100988', '98147', '035', '87472', '76765', '036', '90830', '49368', '037', '49021', '133634', '038', '103317', '59092', '039', '86745', '122657', '040', '102132', '148512', '041', '94590', '128784', '042', '103062', '32565', '043', '93475', '173576']}}

I ran your code and the variable state in line 13 is not defined. Also, in the else block of the if statement, you are replacing the list that was previously stored in results[year][letter]. To fix this, just append the value directly to the list (don’t create a new one) like so: results[year][letter].append(value).

Working Code

import csv

results = {}
with open("yearsandvalues.txt", 'r') as f:
    for line in f:
        list = line.split(",")
        year = str.strip(list[0])
        letter = str.strip(list[1])
        for x in range(2, (len(list))):
            value = str.strip(list[x])
            if year not in results:
                results.update({year: {letter: [value]}},)
            elif letter not in results[year].keys():
                results[year].update({letter: [value]})
            else:
                results[year][letter].append(value)
print(results)
Answered By: Iftikhar Ramnandan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.