Ledger Yu Ledger Yu - 25 days ago 6
Python Question

Element by element concatenation in Python

Say I have two lists:

header = ['a', 'b', 'c', 'd']
data_type = ['str', 'str', 'float64', 'float64']


How do I get a combined list like this:

data_type = {'a':str, 'b':str, 'c':float64, 'd':float64}


This is used to define
dtype
in
pd.read_csv
method.

Answer

Fastest:

header = ['a', 'b', 'c', 'd']
data_type = ['str', 'str', 'float64', 'float64']
dict(zip(header, data_type))

the idea is: two lists merged with zip function(https://docs.python.org/2/library/functions.html#zip) and than produced tuple of tuples converted to dictionary with dict function.