list to map?

Dan Schmidt dfan at harmonixmusic.com
Tue May 18 18:18:03 EDT 1999


bruce.guenter at qcc.sk.ca (Bruce Guenter) writes:

| Greetings.
| 
| I have a list of two-item tuples:
| 	x=[(1,2),(3,4),(5,6)...]
| Is there a built-in way to turn this into a map:
| 	{1:2,3:4,5:6...}
| I have done the obvious of looping over the tuples and adding them to a
| map, but was wondering if there was a different way.

OK, here's a different way.  Look, ma, no looping!

  from string import replace
  x=[(1,2),(3,4),(5,6)]
  exec ("d = " + replace (
                     replace (
                         replace (
                             replace (
                                 replace (
                                     replace (
                                         replace (
                                             `x`,
                                             '),', '\001'),
                                         '(', ''),
                                     '[', '{'),
                                 ']', '}'),
                             ',', ':'),
                         '\001', ','),
                     ')}', '}'))

-- 
                 Dan Schmidt -> dfan at harmonixmusic.com, dfan at alum.mit.edu
Honest Bob & the                http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
          Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/




More information about the Python-list mailing list