Mailing List Archive

Merging two lists as a dictionary
--u3/rZRmxL6MmkK24
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

I have two lists and would like to merge them into a dictionary, with one
list forming the keywords and the other the values in the dictionary. How c=
ould
this be done? "map(None, keyword_list, value_list)" looked great, but it
returns a list of pairs (I'd like to be able to specify values according to
keywords...).

thanks for any pointers!
--=20
Chris Frost | <http://www.frostnet.advicom.net/chris/>
-------------+------------------------------------------
Public PGP Key:
Email chris@frostnet.advicom.net with the subject "retrieve pgp key"
or visit <http://www.frostnet.advicom.net/chris/about/pgp_key.phtml>

--u3/rZRmxL6MmkK24
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
MessageID: yJLS0b1o0SDM/CahccBkTI+EWlkmdVK/

iQA/AwUBN5UgMeEzIlbKpewXEQIn/QCgjGkOxyvgugyls+Q2BgzaoIGkNXAAnjAM
1ZfgVpXDkv7oVJm1qqWARDlC
=MmtE
-----END PGP SIGNATURE-----

--u3/rZRmxL6MmkK24--
Merging two lists as a dictionary [ In reply to ]
Chris Frost writes:
> I have two lists and would like to merge them into a dictionary, with one
> list forming the keywords and the other the values in the dictionary. How could
> this be done? "map(None, keyword_list, value_list)" looked great, but it
> returns a list of pairs (I'd like to be able to specify values according to
> keywords...).

Unless I'm missing the point (which happens more than I'd like to
admit!) I think what you're looking for is just:

dicp={}
for key,value in map(None,key_list,value_list):
dict[key]=value
Merging two lists as a dictionary [ In reply to ]
-->"Chris" == Chris Frost <chris@frostnet.advicom.net> writes:

Chris> I have two lists and would like to merge them into a
Chris> dictionary, with one list forming the keywords and the other
Chris> the values in the dictionary. How c= ould this be done?
Chris> "map(None, keyword_list, value_list)" looked great, but it
Chris> returns a list of pairs (I'd like to be able to specify
Chris> values according to keywords...).

it's ugly, but ...

reduce(lambda d,i,setitem=operator.__setitem__: setitem(d,i[0],i[1]) or d, map(None, keyword_list, value_list), {})




d
Merging two lists as a dictionary [ In reply to ]
From: David Arnold <arnold@dstc.edu.au>
Subject: Re: Merging two lists as a dictionary
Organization: CWI, Amsterdam

-->"Chris" == Chris Frost <chris@frostnet.advicom.net> writes:

Chris> I have two lists and would like to merge them into a
Chris> dictionary, with one list forming the keywords and the other
Chris> the values in the dictionary. How c= ould this be done?
Chris> "map(None, keyword_list, value_list)" looked great, but it
Chris> returns a list of pairs (I'd like to be able to specify
Chris> values according to keywords...).

it's ugly, but ...

reduce(lambda d,i,setitem=operator.__setitem__: setitem(d,i[0],i[1]) or d,
map(None, keyword_list, value_list), {})




d
--
|Fidonet: UUCP 2:500/3.1
|Internet: UUCP@p1.f3.n500.z2.hccfido.hcc.nl
|
| Standard disclaimer: The views of this user are strictly his own.
Merging two lists as a dictionary [ In reply to ]
Hi All--

Chris Frost wrote:
>
> I have two lists and would like to merge them into a dictionary, with one
> list forming the keywords and the other the values in the dictionary. How could
> this be done? "map(None, keyword_list, value_list)" looked great, but it
> returns a list of pairs (I'd like to be able to specify values according to
> keywords...).
>
> thanks for any pointers!

Here's a simplistic implementation:
----------------------------------------------
#!/usr/local/bin/python

k = ["one","two","three"]
v = [1,2,3]
d = {}

n = 0
for i in k:
d[i] = v[n]
n = n + 1

print "Keys:", k
print "Values:", v
print "Dictionary:", d
----------------------------------------------

Alternatively, you could take the list of pairs (tuples) you get back
from map() and iterate over that:

for i in pairs:
dict[i[0]] = i[1]

<you're-in-trouble-if-the-lists-aren't-the-same-length>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
ivanlan@home.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
Merging two lists as a dictionary [ In reply to ]
From: Ivan Van Laningham <ivanlan@callware.com>
Subject: Re: Merging two lists as a dictionary
Organization: Callware Technologies, Inc.

Hi All--

Chris Frost wrote:
>
> I have two lists and would like to merge them into a dictionary, with one
> list forming the keywords and the other the values in the dictionary. How
could
> this be done? "map(None, keyword_list, value_list)" looked great, but it
> returns a list of pairs (I'd like to be able to specify values according to
> keywords...).
>
> thanks for any pointers!

Here's a simplistic implementation:
----------------------------------------------
#!/usr/local/bin/python

k = ["one","two","three"]
v = [1,2,3]
d = {}

n = 0
for i in k:
d[i] = v[n]
n = n + 1

print "Keys:", k
print "Values:", v
print "Dictionary:", d
----------------------------------------------

Alternatively, you could take the list of pairs (tuples) you get back
from map() and iterate over that:

for i in pairs:
dict[i[0]] = i[1]

<you're-in-trouble-if-the-lists-aren't-the-same-length>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
ivanlan@home.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
--
|Fidonet: UUCP 2:500/3.1
|Internet: UUCP@p1.f3.n500.z2.hccfido.hcc.nl
|
| Standard disclaimer: The views of this user are strictly his own.
Merging two lists as a dictionary [ In reply to ]
--WYTEVAkct0FjGQmd
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

On Tue, Jul 20, 1999 at 10:13:16PM -0500, Charles G Waldman wrote:
> Unless I'm missing the point (which happens more than I'd like to
> admit!) I think what you're looking for is just:
>=20
> dicp=3D{}
> for key,value in map(None,key_list,value_list):
> dict[key]=3Dvalue
Perfect! (I wasn't sure on how to do the last line...)

thanks,
--=20
Chris Frost | <http://www.frostnet.advicom.net/chris/>
-------------+------------------------------------------
Public PGP Key:
Email chris@frostnet.advicom.net with the subject "retrieve pgp key"
or visit <http://www.frostnet.advicom.net/chris/about/pgp_key.phtml>

--WYTEVAkct0FjGQmd
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
MessageID: oaNf7P6KTtnUcpD1eNY3QHsNdwqBzWQu

iQA/AwUBN5U56eEzIlbKpewXEQL5UACg2SL8jRxomLBAmqPBNrw5h0YsG+8AoO6K
VapekeQki06VsX9QGWNytpzO
=6Ag0
-----END PGP SIGNATURE-----

--WYTEVAkct0FjGQmd--
Merging two lists as a dictionary [ In reply to ]
On Wed, Jul 21, 1999 at 02:19:46AM +0000, Chris Frost wrote:

> I have two lists and would like to merge them into a dictionary, with one
> list forming the keywords and the other the values in the dictionary. How could
> this be done? "map(None, keyword_list, value_list)" looked great, but it
> returns a list of pairs (I'd like to be able to specify values according to
> keywords...).

>>> keywords = ['s','p','a','m']
>>> values = ['eggs', 'ham', 'toast', 'omelet']
>>> result = {}
>>> for i in range(min(len(keywords), len(values))):
... result[keywords[i]] = values[i]
...
>>> result
{'m': 'omelet', 's': 'eggs', 'p': 'ham', 'a': 'toast'}

the 'min()' is unecessary if you are 100% certain the lists are same length,
or if you know which list will be the shorter one. (iter over the shortest
one, in that case)

--
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
Merging two lists as a dictionary [ In reply to ]
--Fig2xvG2VGoz8o/s
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

On Wed, Jul 21, 1999 at 03:35:39PM +0200, Thomas Wouters wrote:
> >>> keywords =3D ['s','p','a','m'] =20
> >>> values =3D ['eggs', 'ham', 'toast', 'omelet']
> >>> result =3D {}
> >>> for i in range(min(len(keywords), len(values))):
> ... result[keywords[i]] =3D values[i]
> ...=20
> >>> result
> {'m': 'omelet', 's': 'eggs', 'p': 'ham', 'a': 'toast'}
>=20
> the 'min()' is unecessary if you are 100% certain the lists are same leng=
th,
> or if you know which list will be the shorter one. (iter over the shortest
> one, in that case)
Great, that works better than my error checking method.

--=20
Chris Frost | <http://www.frostnet.advicom.net/chris/>
-------------+------------------------------------------
Public PGP Key:
Email chris@frostnet.advicom.net with the subject "retrieve pgp key"
or visit <http://www.frostnet.advicom.net/chris/about/pgp_key.phtml>

--Fig2xvG2VGoz8o/s
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
MessageID: MyN4NI6EXPBiODnSN47wm+sP+3SXNDzL

iQA/AwUBN5XBFeEzIlbKpewXEQLBpACguMr/cLY+zULxa7cdGEdKoMt9sdAAoKR8
mH/OcH67PrJvfa4h+GOmhiAZ
=1P0Z
-----END PGP SIGNATURE-----

--Fig2xvG2VGoz8o/s--
Merging two lists as a dictionary [ In reply to ]
Would

import operator
destination={}
assert len(list_of_keys)==len(list_of_values)
map(operator.setitem,len(list_of_keys)*[destination],list_of_keys,
list_of_values)

do the trick?

Maybe the "for" loop way is faster, though.

Alex.
Merging two lists as a dictionary [ In reply to ]
Can anyone tell me why all of Chris's posts are showing up as
attachments at my end? (Outlook Express 5, win95b)

--

Emile van Sebille
emile@fenx.com
-------------------


Chris Frost <chris@frostnet.advicom.net> wrote in message
news:19990721084614.C15502@pooh.frostnet.net...
Merging two lists as a dictionary [ In reply to ]
I think it's because you have to have Tools->Options->send... News - plain
text selected, not HTML?

N

Emile van Sebille <emile@fenx.com> wrote in message
news:T%kl3.3903$VL2.416831@news.direcpc.com...
>Can anyone tell me why all of Chris's posts are showing up as
>attachments at my end? (Outlook Express 5, win95b)
>
>--
>
>Emile van Sebille
>emile@fenx.com
>-------------------
>
>
>Chris Frost <chris@frostnet.advicom.net> wrote in message
>news:19990721084614.C15502@pooh.frostnet.net...
>
>
Merging two lists as a dictionary [ In reply to ]
Emile van Sebille <emile@fenx.com> wrote:
> Can anyone tell me why all of Chris's posts are showing up as
> attachments at my end? (Outlook Express 5, win95b)

he's posting "multipart/signed" messages, by some reason.
looks like outlook express cannot deal with that.

(you can read his posts using ctrl-F3, btw...)

</F>
Merging two lists as a dictionary [ In reply to ]
I think there's a C function in mxTools, as well.
Sue.