Re: Dictionary from a list

2009-08-23 Thread Aahz
In article , EK 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 reada

Re: Dictionary from a list

2009-08-22 Thread EK
On Aug 20, 2:10 pm, Peter Otten <__pete...@web.de> wrote: > Jan Kaliszewski wrote: > > 20-08-2009 o 02:05:57 Jan Kaliszewski wrote: > > >> Or probably better: > > >>      from itertools import islice, izip > >>      dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2))) > > > Or similarly, per

Re: Dictionary from a list

2009-08-20 Thread iu2
On Aug 20, 9:10 am, Peter Otten <__pete...@web.de> wrote: > Jan Kaliszewski wrote: > > 20-08-2009 o 02:05:57 Jan Kaliszewski wrote: > > >> Or probably better: > > >>      from itertools import islice, izip > >>      dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2))) > > > Or similarly, per

Re: Dictionary from a list

2009-08-20 Thread Peter Otten
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. >

Re: Dictionary from a list

2009-08-20 Thread Tim Chase
Peter Otten wrote: it = iter([1,2,3,4,5,6]) dict(izip(it, it)) {1: 2, 3: 4, 5: 6} Zip(it). Zip(it) good. it's-3:00am-and-i-seriously-need-to-sleep'ly yers... -tkc -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary from a list

2009-08-20 Thread Steven D'Aprano
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 ove

Re: Dictionary from a list

2009-08-19 Thread Peter Otten
Jan Kaliszewski wrote: > 20-08-2009 o 02:05:57 Jan Kaliszewski 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((ite

Re: Dictionary from a list

2009-08-19 Thread Jan Kaliszewski
20-08-2009 o 02:05:57 Jan Kaliszewski 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(l

Re: Dictionary from a list

2009-08-19 Thread Jan Kaliszewski
19-08-2009 o 22:52:54 iu2 wrote: On Aug 19, 11:39 pm, "Diez B. Roggisch" 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

Re: Dictionary from a list

2009-08-19 Thread iu2
On Aug 19, 11:39 pm, "Diez B. Roggisch" 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])) > > Diez Wow, this is cool! thanks iu2 -- http://mai

Re: Dictionary from a list

2009-08-19 Thread Diez B. Roggisch
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 -- http://mail.python.org/mailman/listinfo/python-list

Dictionary from a list

2009-08-19 Thread iu2
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Duncan Booth
"Wim Vogelaar" wrote: > >> >> why this output isn't ordered, giving: >> {2: 3, 4: 5, 6: 7, 8: 9, 10: 11 } >> >> > > I made the original list two elements longer: a = > [1,2,3,4,5,6,7,8,9,10,11,12] > > and to my surprise the output is now ordered, giving: {2: 3, 4: 5, 6: > 7, 8: 9, 10: 11, 12:

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Gabriel Genellina
En Mon, 28 May 2007 05:37:12 -0300, Wim Vogelaar <[EMAIL PROTECTED]> escribió: > I made the original list two elements longer: a = > [1,2,3,4,5,6,7,8,9,10,11,12] > > and to my surprise the output is now ordered, giving: {2: 3, 4: 5, 6: 7, > 8: > 9, 10: 11, 12: 13} > > I am running ActiveState

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Peter Otten
[EMAIL PROTECTED] wrote: > Do you think we just shouldn't use list comprehensions to build > dictinaries at all? Or is Stefan's solution acceptable (and pythonic)? Use list comprehensions where you need the resulting list; if you want nothing but the side effects, use a for loop. [Stefan Sonnenb

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread half . italian
On May 28, 12:25 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, half.italian > wrote: > > > [entries.__setitem__(int(d.date.strftime('%m'))], d.id) for d in > > links] > > > btw...I was curious of this too. I used 'dir(dict)' and looked for a > > method that migh

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Wim Vogelaar
> > why this output isn't ordered, giving: > {2: 3, 4: 5, 6: 7, 8: 9, 10: 11 } > > I made the original list two elements longer: a = [1,2,3,4,5,6,7,8,9,10,11,12] and to my surprise the output is now ordered, giving: {2: 3, 4: 5, 6: 7, 8: 9, 10: 11, 12: 13} I am running ActiveState ActivePytho

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Gabriel Genellina
En Mon, 28 May 2007 05:20:16 -0300, Wim Vogelaar <[EMAIL PROTECTED]> escribió: >> Example: >> >> a = [1,2,3,4,5,6,7,8,9,10] >> >> aDict = dict([(x,x+1) for x in a if x%2==0]) >> >> print aDict >> > > When I run this program I get: > {8: 9, 2: 3, 4: 5, 10: 11, 6: 7} > > why this output isn't orde

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Maric Michaud
Pierre Quentel a écrit : > On 27 mai, 22:55, erikcw <[EMAIL PROTECTED]> wrote: >> Hi, >> >> I'm trying to turn o list of objects into a dictionary using a list >> comprehension. ... > > entries = dict([ (int(d.date.strftime('%m')),d.id) for d in links] ) > > With Python2.4 and above you can use a

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Wim Vogelaar
> Example: > > a = [1,2,3,4,5,6,7,8,9,10] > > aDict = dict([(x,x+1) for x in a if x%2==0]) > > print aDict > When I run this program I get: {8: 9, 2: 3, 4: 5, 10: 11, 6: 7} why this output isn't ordered, giving: {2: 3, 4: 5, 6: 7, 8: 9, 10: 11 } -- http://mail.python.org/mailman/listinfo/pyth

Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, half.italian wrote: > [entries.__setitem__(int(d.date.strftime('%m'))], d.id) for d in > links] > > btw...I was curious of this too. I used 'dir(dict)' and looked for a > method that might do what we wanted and bingo! This is really ugly. Except `__init__()` it's always

Re: Can python create a dictionary from a list comprehension?

2007-05-27 Thread Pierre Quentel
On 27 mai, 22:55, erikcw <[EMAIL PROTECTED]> wrote: > Hi, > > I'm trying to turn o list of objects into a dictionary using a list > comprehension. > > Something like > > entries = {} > [entries[int(d.date.strftime('%m'))] = d.id] for d in links] > > I keep getting errors when I try to do it. Is i

Re: Can python create a dictionary from a list comprehension?

2007-05-27 Thread Stefan Sonnenberg-Carstens
erikcw schrieb: > Hi, > > I'm trying to turn o list of objects into a dictionary using a list > comprehension. > > Something like > > entries = {} > [entries[int(d.date.strftime('%m'))] = d.id] for d in links] > > I keep getting errors when I try to do it. Is it possible? Do > dictionary objects

Re: Can python create a dictionary from a list comprehension?

2007-05-27 Thread half . italian
On May 27, 1:55 pm, erikcw <[EMAIL PROTECTED]> wrote: > Hi, > > I'm trying to turn o list of objects into a dictionary using a list > comprehension. > > Something like > > entries = {} > [entries[int(d.date.strftime('%m'))] = d.id] for d in links] > > I keep getting errors when I try to do it. Is

Can python create a dictionary from a list comprehension?

2007-05-27 Thread erikcw
Hi, I'm trying to turn o list of objects into a dictionary using a list comprehension. Something like entries = {} [entries[int(d.date.strftime('%m'))] = d.id] for d in links] I keep getting errors when I try to do it. Is it possible? Do dictionary objects have a method equivalent to [].appe

Re: a dictionary from a list

2005-06-27 Thread Raymond Hettinger
[Roy Smith] > I also think the published description is needlessly confusing. Why does > it use > >{'one': 2, 'two': 3} > > as the example mapping when > >{'one': 1, 'two': 2} > > would illustrate exactly the same point but be easier to comprehend. The > mapping given is the kind of thing

Re: a dictionary from a list

2005-06-27 Thread Raymond Hettinger
[Roy Smith] > I also think the published description is needlessly confusing. Why does > it use > >{'one': 2, 'two': 3} > > as the example mapping when > >{'one': 1, 'two': 2} > > would illustrate exactly the same point but be easier to comprehend. The > mapping given is the kind of thing

Re: a dictionary from a list

2005-06-25 Thread George Sakkis
"Steven D'Aprano" wrote: > On Sat, 25 Jun 2005 06:44:22 -0700, George Sakkis wrote: > > > "Roy Smith" <[EMAIL PROTECTED]> wrote: > > > >> I just re-read the documentation on the dict() constructor. Why does it > >> support keyword arguments? > >> > >>dict(foo="bar", baz="blah") ==> {"foo":"ba

Re: a dictionary from a list

2005-06-25 Thread Steven D'Aprano
On Sat, 25 Jun 2005 06:44:22 -0700, George Sakkis wrote: > "Roy Smith" <[EMAIL PROTECTED]> wrote: > >> I just re-read the documentation on the dict() constructor. Why does it >> support keyword arguments? >> >>dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz"="blah"} >> >> This smacks of c

Re: a dictionary from a list

2005-06-25 Thread Scott David Daniels
Roy Smith wrote: > Terry Hancock <[EMAIL PROTECTED]> wrote: > ... > I just re-read the documentation on the dict() constructor. Why does it > support keyword arguments? > >dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz"="blah"} > > This smacks of creeping featurism. Is this actually us

Re: a dictionary from a list

2005-06-25 Thread Michael Hoffman
Roy Smith wrote: > I just re-read the documentation on the dict() constructor. Why does it > support keyword arguments? > >dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz"="blah"} > > This smacks of creeping featurism. Is this actually useful in real code? Personally, I use it all t

Re: a dictionary from a list

2005-06-25 Thread Jp Calderone
On Sat, 25 Jun 2005 09:10:33 -0400, Roy Smith <[EMAIL PROTECTED]> wrote: >Terry Hancock <[EMAIL PROTECTED]> wrote: >> Before the dict constructor, you needed to do this: >> >> d={} >> for key in alist: >> d[key]=None > >I just re-read the documentation on the dict() constructor. Why does it >s

Re: a dictionary from a list

2005-06-25 Thread George Sakkis
"Roy Smith" <[EMAIL PROTECTED]> wrote: > I just re-read the documentation on the dict() constructor. Why does it > support keyword arguments? > >dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz"="blah"} > > This smacks of creeping featurism. Is this actually useful in real code? > It took

Re: a dictionary from a list

2005-06-25 Thread Roy Smith
Terry Hancock <[EMAIL PROTECTED]> wrote: > Before the dict constructor, you needed to do this: > > d={} > for key in alist: > d[key]=None I just re-read the documentation on the dict() constructor. Why does it support keyword arguments? dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz

Re: a dictionary from a list

2005-06-25 Thread Terry Hancock
On Friday 24 June 2005 05:26 pm, infidel wrote: > dict((x, None) for x in alist) or if you want it to run in 2.3 (before "generator expressions"): dict( [(x,None) for x in alist] ) Before the dict constructor, you needed to do this: d={} for key in alist: d[key]=None which is still only 3

Re: a dictionary from a list

2005-06-24 Thread Peter Hansen
Dave Cook wrote: > On 2005-06-24, infidel <[EMAIL PROTECTED]> wrote: > > >>dict((x, None) for x in alist) > > Whoa, I thought dictionary comprehensions were still planned feature. I > guess I gotta start paying closer attention. Added in Python 2.4, it's actually a generator expression as the

Re: a dictionary from a list

2005-06-24 Thread Dave Cook
On 2005-06-24, infidel <[EMAIL PROTECTED]> wrote: > dict((x, None) for x in alist) Whoa, I thought dictionary comprehensions were still planned feature. I guess I gotta start paying closer attention. Dave Cook -- http://mail.python.org/mailman/listinfo/python-list

Re: a dictionary from a list

2005-06-24 Thread George Sakkis
"Rocco Moretti" wrote: > Are you sure you need a dictionary? You may want to look at the Set > module instead, if the values aren't important. Set is the name of the type in the module sets, introduced in 2.3. Since 2.4 you can use the builtin set type. Here's the import snippet that works for 2.

Re: a dictionary from a list

2005-06-24 Thread Rocco Moretti
David Bear wrote: > I know there must be a better way to phrase this so google understands, but > I don't know how.. So I'll ask people. > > Assume I have a list object called 'alist'. > > Is there an easy way to create a dictionary object with the members of > 'alist' being the keys in the dicti

Re: a dictionary from a list

2005-06-24 Thread Leif K-Brooks
David Bear wrote: > Is there an easy way to create a dictionary object with the members of > 'alist' being the keys in the dictionary, and the value of the keys set to > null? adict = dict.fromkeys(alist) -- http://mail.python.org/mailman/listinfo/python-list

Re: a dictionary from a list

2005-06-24 Thread infidel
dict((x, None) for x in alist) -- http://mail.python.org/mailman/listinfo/python-list

Re: a dictionary from a list

2005-06-24 Thread Benji York
David Bear wrote: > Assume I have a list object called 'alist'. > > Is there an easy way to create a dictionary object with the members of > 'alist' being the keys in the dictionary, and the value of the keys set to > null? You mean None, right? :) >>> a_list = [1, 2, 3, 'a', 'b', 'c'] >>> di

a dictionary from a list

2005-06-24 Thread David Bear
I know there must be a better way to phrase this so google understands, but I don't know how.. So I'll ask people. Assume I have a list object called 'alist'. Is there an easy way to create a dictionary object with the members of 'alist' being the keys in the dictionary, and the value of the keys