Merging two lists as a dictionary

Chris Frost chris at frostnet.advicom.net
Wed Jul 21 09:46:14 EDT 1999


On Wed, Jul 21, 1999 at 03:35:39PM +0200, Thomas Wouters wrote:
> >>> 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)
Great, that works better than my error checking method.

-- 
Chris Frost  |  <http://www.frostnet.advicom.net/chris/>
-------------+------------------------------------------
Public PGP Key:
   Email chris at frostnet.advicom.net with the subject "retrieve pgp key"
   or visit <http://www.frostnet.advicom.net/chris/about/pgp_key.phtml>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 256 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/19990721/4427a611/attachment.sig>


More information about the Python-list mailing list