dict initialization

2009-12-22 Thread mattia
Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? -- http://mail.python.org/mailman/listinfo/python-list

Re: dict initialization

2009-12-22 Thread Robert Kern
On 2009-12-22 15:33 PM, mattia wrote: Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? For things like this? No. If you find yourself writing this pattern frequently, though, you can wrap it up in a function and

Re: dict initialization

2009-12-22 Thread Emile van Sebille
On 12/22/2009 1:33 PM mattia said... Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? I tend to use setdefault and fill in as I go, but if you need to have a complete 50-element dict from the get go, I'd probably

Re: dict initialization

2009-12-22 Thread Irmen de Jong
On 22-12-2009 22:33, mattia wrote: Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? Maybe you can use: dict.fromkeys(xrange(1,51)) but this will initialize all values to None instead of an empty list... -

Re: dict initialization

2009-12-22 Thread Mark Tolonen
"mattia" wrote in message news:4b313b3a$0$1135$4fafb...@reader1.news.tin.it... Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? Depending on your use case, a defaultdict might suite you: from collections impo

Re: dict initialization

2009-12-22 Thread Peter Otten
mattia wrote: > Is there a function to initialize a dictionary? > Right now I'm using: > d = {x+1:[] for x in range(50)} > Is there any better solution? There is a dictionary variant that you don't have to initialize: from collections import defaultdict d = defaultdict(list) Peter -- http://ma

Re: dict initialization

2009-12-22 Thread mattia
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: > mattia wrote: > >> Is there a function to initialize a dictionary? Right now I'm using: >> d = {x+1:[] for x in range(50)} >> Is there any better solution? > > There is a dictionary variant that you don't have to initialize: > > from

Re: dict initialization

2009-12-22 Thread mattia
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: > mattia wrote: > >> Is there a function to initialize a dictionary? Right now I'm using: >> d = {x+1:[] for x in range(50)} >> Is there any better solution? > > There is a dictionary variant that you don't have to initialize: > > from

Re: dict initialization

2009-12-22 Thread Jon Clements
On Dec 22, 11:51 pm, mattia wrote: > Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: > > > mattia wrote: > > >> Is there a function to initialize a dictionary? Right now I'm using: > >> d = {x+1:[] for x in range(50)} > >> Is there any better solution? > > > There is a dictionary varia

Re: dict initialization

2009-12-22 Thread MRAB
mattia wrote: Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: mattia wrote: Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? There is a dictionary variant that you don't have to initialize: from co

Proposal: named return values through dict initialization and unpacking

2016-06-21 Thread Ari Freund via Python-list
I'd like to run this idea by the community to see if it's PEP worthy and hasn't been already rejected. Background Just as keyword arguments enhance code readability and diminish the risk of bugs, so too would named return values. Currently, we can write val1, val2, val3 = myfunc() but we must

Re: Proposal: named return values through dict initialization and unpacking

2016-06-21 Thread Terry Reedy
On 6/21/2016 3:34 AM, Ari Freund via Python-list wrote: I'd like to run this idea by the community to see if it's PEP worthy and hasn't been already rejected. Background Just as keyword arguments enhance code readability and diminish the risk of bugs, so too would named return values. Currently

Re: Proposal: named return values through dict initialization and unpacking

2016-06-21 Thread MRAB
On 2016-06-21 18:12, Terry Reedy wrote: On 6/21/2016 3:34 AM, Ari Freund via Python-list wrote: I'd like to run this idea by the community to see if it's PEP worthy and hasn't been already rejected. Background Just as keyword arguments enhance code readability and diminish the risk of bugs, so

Re: Proposal: named return values through dict initialization and unpacking

2016-06-21 Thread Michael Selik
On Tue, Jun 21, 2016, 10:14 AM Terry Reedy wrote: > On 6/21/2016 3:34 AM, Ari Freund via Python-list wrote: > > I'd like to run this idea by the community to see if it's PEP worthy and > > hasn't been already rejected. > > > There was a recent (last couple of months?) discussion on python-ideas >

Re: Proposal: named return values through dict initialization and unpacking

2016-06-21 Thread Steven D'Aprano
On Tue, 21 Jun 2016 05:34 pm, Ari Freund wrote: > I'd like to run this idea by the community to see if it's PEP worthy and > hasn't been already rejected. > > Background > Just as keyword arguments enhance code readability and diminish the risk > of bugs, so too would named > return values. I do

Re: Proposal: named return values through dict initialization and unpacking

2016-06-26 Thread Ari Freund via Python-list
Thanks everybody. There seems to be a lot of resistance to dict unpacking, in addition to the problem with my proposed shorthand dict() initialization syntax pointed out by Steven D'Aprano, so I won't be pursuing this. -- https://mail.python.org/mailman/listinfo/python-list

Re: Proposal: named return values through dict initialization and unpacking

2016-06-26 Thread Joonas Liik
On 26 June 2016 at 18:28, Ari Freund via Python-list wrote: > Thanks everybody. There seems to be a lot of resistance to dict unpacking, > in addition to the problem with my proposed shorthand dict() initialization > syntax pointed out by Steven D'Aprano, so I won'

Re: Proposal: named return values through dict initialization and unpacking

2016-06-26 Thread Michael Selik
On Tue, Jun 21, 2016 at 9:41 PM Steven D'Aprano wrote: > On Tue, 21 Jun 2016 05:34 pm, Ari Freund wrote: > > var3, var1, var2 = **d > > But I don't want to use the key names your function uses. I want to > use names which makes sense for my application > Note that my dict unpacking synt