1

I am having difficulty understanding the syntactic workings of line 7:

import csv

DATA_FILE = "owid-covid-data.csv"

input_file = csv.DictReader(open(DATA_FILE))
fieldnames = input_file.fieldnames
data_dict = {fn: [] for fn in fieldnames}

for line in input_file:
    for k, v in line.items():
        if (v == ''): #quick fix for missing values
            v=0
        try:
            data_dict[k].append(int(v))
        except ValueError:
            try:
                data_dict[k].append(float(v))
            except ValueError:
                data_dict[k].append(v)
                
for k, v in data_dict.items():
    data_dict[k] = np.array(v)

specifically:

fn: []

as well as the existence of a for loop within the 'data_dict' dictionary's initiation

{fn: [] for fn in fieldnames}

I am a c++ programmer, however I am just now learning Python; thus any direction to external documentation on the topic, as well as a comparative analogy in c/c++ (or another c-family language for that matter) would be greatly appreciated. Thank you.

anthdono
  • 21
  • 3
  • 1
    The key is the value of `fn` and the value is an empty list. In other words, `dictionary[fn] = []` where however the `dictionary` is not a named variable here. – tripleee Mar 17 '21 at 07:09

0 Answers0