Creating dict from keys and values

D

Dirkjan Ochtman

Hi there,

I'm looking for an intuitive way of creating a dict from what the
dict.keys() and dict.values() will return (two lists).

Currently, I'm using this code:
d = {}
for i in range(len(data)):
d[header] = data


But this feels kind of inelegant. So: is there a better way?

TIA,

Dirkjan
 
J

Just

Dirkjan Ochtman said:
Hi there,

I'm looking for an intuitive way of creating a dict from what the
dict.keys() and dict.values() will return (two lists).

Currently, I'm using this code:
d = {}
for i in range(len(data)):
d[header] = data


But this feels kind of inelegant. So: is there a better way?


d = dict(zip(header, data))

Just
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top