'building dictionary from list of column names and list of numbers with a for loop

I am simply trying to build a dictionary with keys from a list of strings and values that area list of numbers.

filepath = path
df = pd.read_excel(filepath, sheet_name=sheet)


columns = df.columns
fields = columns.to_list()
values = [1,2,3]
dict = {}

def build_dict(fields,values):
    for i in fields:
        dict[i] = fields[i]
    print(dict)

build_dict(fields,values)

I consistently end up with... TypeError: list indices must be integers or slices, not str



Solution 1:[1]

You can make a dictionary from two lists as follows:

filepath = path
df = pd.read_excel(filepath, sheet_name=sheet)


columns = df.columns
fields = columns.to_list()
values = [1,2,3]
dict = dict(zip(fields, values))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyarel