Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dictionary from a list

6 views
Skip to first unread message

iu2

unread,
Aug 19, 2009, 4:31:17 PM8/19/09
to
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}

I'll appreciate your help
Thanks
iu2

Diez B. Roggisch

unread,
Aug 19, 2009, 4:39:47 PM8/19/09
to
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]))

Diez

iu2

unread,
Aug 19, 2009, 4:52:54 PM8/19/09
to

Wow, this is cool!

thanks
iu2

Jan Kaliszewski

unread,
Aug 19, 2009, 8:05:57 PM8/19/09
to iu2, pytho...@python.org
19-08-2009 o 22:52:54 iu2 <isr...@elbit.co.il> wrote:

> 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>

Jan Kaliszewski

unread,
Aug 19, 2009, 8:29:32 PM8/19/09
to Jan Kaliszewski, iu2, pytho...@python.org
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))

Peter Otten

unread,
Aug 20, 2009, 2:10:28 AM8/20/09
to
Jan Kaliszewski wrote:

> 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

Steven D'Aprano

unread,
Aug 20, 2009, 3:05:41 AM8/20/09
to
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:


>>> 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

Peter Otten

unread,
Aug 20, 2009, 3:45:37 AM8/20/09
to
Steven D'Aprano wrote:

> 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 ;)

Tim Chase

unread,
Aug 20, 2009, 3:47:25 AM8/20/09
to Peter Otten, pytho...@python.org
Peter Otten wrote:
>>>> it = iter([1,2,3,4,5,6])
>>>> dict(izip(it, it))
> {1: 2, 3: 4, 5: 6}

<devo>Zip(it). Zip(it) good.</devo>

it's-3:00am-and-i-seriously-need-to-sleep'ly yers...

-tkc

iu2

unread,
Aug 20, 2009, 12:41:14 PM8/20/09
to

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

EK

unread,
Aug 22, 2009, 6:03:27 AM8/22/09
to

dict(zip(*[iter(l)]*2))

Aahz

unread,
Aug 23, 2009, 9:11:05 AM8/23/09
to
In article <baa0d11c-c999-4eee...@b25g2000prb.googlegroups.com>,
EK <eka...@gmail.com> wrote:

>On Aug 20, 2:10=A0pm, Peter Otten <__pete...@web.de> wrote:
>>
>> >>> from itertools import izip
>> >>> it =3D iter([1,2,3,4,5,6])

>> >>> dict(izip(it, it))
>>
>> {1: 2, 3: 4, 5: 6}
>
>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

0 new messages