Making a dict from two lists/tuples

Andrew Stribblehill a.d.stribblehill at durham.ac.uk
Thu May 24 09:02:11 EDT 2001


"Jonas Meyer Rasmussen" <meyer at kampsax.dtu.dk> writes:

> for key,value in keys,values
>     dict[key] = value

ObNetiquette: Replying after the post is the convention, so people can
read the message top-to-bottom in chronological order.

This was half of my solution. Obviously, taken naively, this makes a
dict like {'foo': 'bar', 1: 2}. So I set kvlist = zip(keys, values)
before I did the above 'for' expression.

> "Andrew Stribblehill" <a.d.stribblehill at durham.ac.uk> wrote in message
> news:87u22bw197.fsf at womble.dur.ac.uk...
> >
> > I want a neat way to turn
> >
> > keys = ('foo', 'bar')
> > values = (1, 2)
> >
> > into
> >
> > dict = {'foo': 1, 'bar': 2}.
> >
> >
> > I'm sure there's some (possibly-functional) idiom to do this in one
> > simple line, but I'm totally unable to work out what!

-- 
Andrew Stribblehill
Systems programmer, IT Service, University of Durham, England



More information about the Python-list mailing list