Reputation: 1690
I have a list like this:
destinations = ['one', 'two', 'three']
. I have an input string that I am splitting like this: one, two, three = re.split(r'\t', line)
. What I'd like to do is have the split operation fill a dictionary, with the destinations
entries being the keys of the dictionary, and the fields from the line being the values. Is this possible with python?
Upvotes: 0
Views: 42
Reputation: 1124798
Use the zip()
function to pair up destinations
and the output of re.split()
into key-value pairs, then pass that to dict()
:
dictionary = dict(zip(destinations, re.split(r'\t', line)))
I suspect you are trying to read tab-separated CSV data; try not to reinvent the wheel and use the csv.DictReader()
class instead. It'll even read the fieldnames from the first row of the file, if so required.
Upvotes: 4