I need to create a dictionary out of a list.
Given the list [1, 2, 3, 4, 5, 6]
I need the dictionary: {1:2, 3:4, 5:6}
I'll appreciate your help
Thanks
iu2
dict(zip(l[::2], l[1::2]))
Diez
Wow, this is cool!
thanks
iu2
> On Aug 19, 11:39 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> iu2 schrieb:
>>
>> > Hi all,
>>
>> > I need to create a dictionary out of a list.
>>
>> > Given the list [1, 2, 3, 4, 5, 6]
>>
>> > I need the dictionary: {1:2, 3:4, 5:6}
>>
>> dict(zip(l[::2], l[1::2]))
Or (for long lists, when memory becomes expensive):
dict(li[i:i+2] for i in xrange(0, len(li), 2))
Or probably better:
from itertools import islice, izip
dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))
Cheers,
*j
--
Jan Kaliszewski (zuo) <z...@chopin.edu.pl>
> Or probably better:
>
> from itertools import islice, izip
> dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))
Or similarly, perhaps more readable:
iterator = iter(li)
dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))
> 20-08-2009 o 02:05:57 Jan Kaliszewski <z...@chopin.edu.pl> wrote:
>
>> Or probably better:
>>
>> from itertools import islice, izip
>> dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))
>
> Or similarly, perhaps more readable:
>
> iterator = iter(li)
> dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))
I just can't stop posting this one:
>>> from itertools import izip
>>> it = iter([1,2,3,4,5,6])
>>> dict(izip(it, it))
{1: 2, 3: 4, 5: 6}
I really tried, but yours drove me over the edge.
Peter
> I just can't stop posting this one:
>
>>>> from itertools import izip
>>>> it = iter([1,2,3,4,5,6])
>>>> dict(izip(it, it))
> {1: 2, 3: 4, 5: 6}
>
> I really tried, but yours drove me over the edge.
If you want something to drive you over the edge:
>>> alist = [1, 2, 3, 4, 5, 6]
>>> dict(apply(zip, map(lambda n: map(lambda t: t[1], filter(lambda t:
((not (t[0]%2)) == 1) == n, enumerate(alist))), range(1, -1, -1))))
{1: 2, 3: 4, 5: 6}
Enjoy :)
--
Steven
> On Thu, 20 Aug 2009 08:10:28 +0200, Peter Otten wrote:
>
>
>> I just can't stop posting this one:
>>
>>>>> from itertools import izip
>>>>> it = iter([1,2,3,4,5,6])
>>>>> dict(izip(it, it))
>> {1: 2, 3: 4, 5: 6}
>>
>> I really tried, but yours drove me over the edge.
>
> If you want something to drive you over the edge:
I meant that figuratively...
>>>> alist = [1, 2, 3, 4, 5, 6]
>>>> dict(apply(zip, map(lambda n: map(lambda t: t[1], filter(lambda t:
> ((not (t[0]%2)) == 1) == n, enumerate(alist))), range(1, -1, -1))))
> {1: 2, 3: 4, 5: 6}
...originally.
> Enjoy :)
Not ;)
<devo>Zip(it). Zip(it) good.</devo>
it's-3:00am-and-i-seriously-need-to-sleep'ly yers...
-tkc
Nice.
(but looks like stepping towards the dark side ... :-)
I also liked this one:
iterator = iter(li)
dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))
which inspired me to do something quite similar:
a=range(1, 11)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> dict([[a.pop(0), a.pop(0)] for i in range(len(a)/2)])
{1: 2, 3: 4, 9: 10, 5: 6, 7: 8}
Thanks
dict(zip(*[iter(l)]*2))
No, that's not a good solution. For starters, it's less readable than
Peter's version. More importantly, it has poor memory use because it
needs to construct both the intermediate tuple and the zip() list.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
"I support family values -- Addams family values" --www.nancybuttons.com