Re: list 2 dict?
- From: DevPlayer <devplayer@xxxxxxxxx>
- Date: Mon, 3 Jan 2011 20:34:18 -0800 (PST)
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.
.
- Follow-Ups:
- Re: list 2 dict?
- From: DevPlayer
- Re: list 2 dict?
- References:
- list 2 dict?
- From: Octavian Rasnita
- list 2 dict?
- Prev by Date: Re: CPython on the Web
- Next by Date: Re: CPython on the Web
- Previous by thread: Re: list 2 dict?
- Next by thread: Re: list 2 dict?
- Index(es):