list 2 dict?

DevPlayer devplayer at gmail.com
Mon Jan 3 23:34:18 EST 2011


An adaptation to Hrvoje Niksic's recipe

Use a dictionary comprehention instead of a list comprehension or
function call:

lyst = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']

it = iter( lyst )
dyct = {i:it.next() for i in it}  # I'm using {} and not [] for those
with tiny fonts.

#print dyct
{8: 'b', 1: 2, 3: 4, 5: 6, 7: 'a'}

Of course check for an "even" number of elements to the original list
to avoid exceptions or dropping the last element on traps.



More information about the Python-list mailing list