Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

dict initialization

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


gervaz at gmail

Dec 22, 2009, 1:33 PM

Post #1 of 10 (481 views)
Permalink
dict initialization

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


robert.kern at gmail

Dec 22, 2009, 1:41 PM

Post #2 of 10 (459 views)
Permalink
Re: dict initialization [In reply to]

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 call that function to get your
initialized dicts.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


emile at fenx

Dec 22, 2009, 1:44 PM

Post #3 of 10 (460 views)
Permalink
Re: dict initialization [In reply to]

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 do the same.

>>> D = {}
>>>
>>> import random
>>> for ii in range(20):
... L=D.setdefault(random.randint(0,9),[])
... L.append('.')

Emile

--
http://mail.python.org/mailman/listinfo/python-list


irmen-NOSPAM- at xs4all

Dec 22, 2009, 1:50 PM

Post #4 of 10 (455 views)
Permalink
Re: dict initialization [In reply to]

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

-irmen




--
http://mail.python.org/mailman/listinfo/python-list


metolone+gmane at gmail

Dec 22, 2009, 2:02 PM

Post #5 of 10 (454 views)
Permalink
Re: dict initialization [In reply to]

"mattia" <gervaz [at] gmail> wrote in message
news:4b313b3a$0$1135$4fafbaef [at] reader1
> 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 import defaultdict
>>> D=defaultdict(list)
>>> D[0]
[]
>>> D[49]
[]

If the key doesn't exist, it will be initialized by calling the factory
function provided in the constructor.

-Mark


--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Dec 22, 2009, 2:09 PM

Post #6 of 10 (454 views)
Permalink
Re: dict initialization [In reply to]

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://mail.python.org/mailman/listinfo/python-list


gervaz at gmail

Dec 22, 2009, 2:33 PM

Post #7 of 10 (455 views)
Permalink
Re: dict initialization [In reply to]

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 collections import defaultdict
> d = defaultdict(list)
>
> Peter

Great, thanks. Now when I call the dict key I also initialize the value,
good also using something like:
if d[n]:
d[n].append(val)
--
http://mail.python.org/mailman/listinfo/python-list


gervaz at gmail

Dec 22, 2009, 3:51 PM

Post #8 of 10 (453 views)
Permalink
Re: dict initialization [In reply to]

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 collections import defaultdict
> d = defaultdict(list)
>
> Peter

...and it's also the only way to do something like:
>>> def zero():
... return 0
...
>>> d = defaultdict(zero)
>>> s = ['one', 'two', 'three', 'four', 'two', 'two', 'one']
>>> for x in s:
... d[x] += 1
...
>>> d
defaultdict(<function zero at 0x00BA01E0>, {'four': 1, 'three': 1, 'two':
3, 'one': 2
})
>>>
--
http://mail.python.org/mailman/listinfo/python-list


joncle at googlemail

Dec 22, 2009, 4:10 PM

Post #9 of 10 (455 views)
Permalink
Re: dict initialization [In reply to]

On Dec 22, 11:51 pm, mattia <ger...@gmail.com> 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 collections import defaultdict
> > d = defaultdict(list)
>
> > Peter
>
> ...and it's also the only way to do something like:>>> def zero():
>
> ...     return 0
> ...>>> d = defaultdict(zero)
> >>> s = ['one', 'two', 'three', 'four', 'two', 'two', 'one']
> >>> for x in s:
>
> ...     d[x] += 1
> ...>>> d
>
> defaultdict(<function zero at 0x00BA01E0>, {'four': 1, 'three': 1, 'two':
> 3, 'one': 2
>
> })
>
>

Normally you'd write this defaultdict(int) as int() returns 0 by
default.

Although IIRC, the 3.1 series has a "Counter" class in collections.

Jon.
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Dec 22, 2009, 4:24 PM

Post #10 of 10 (452 views)
Permalink
Re: dict initialization [In reply to]

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 collections import defaultdict
>> d = defaultdict(list)
>>
>> Peter
>
> ...and it's also the only way to do something like:
>>>> def zero():
> ... return 0
> ...
>>>> d = defaultdict(zero)

In this case it's probably more Pythonic to do it this way:

>>> d = defaultdict(int)

>>>> s = ['one', 'two', 'three', 'four', 'two', 'two', 'one']
>>>> for x in s:
> ... d[x] += 1
> ...
>>>> d
> defaultdict(<function zero at 0x00BA01E0>, {'four': 1, 'three': 1, 'two':
> 3, 'one': 2
> })

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.