Init a dictionary with a empty lists

  • Thread starter Lisa Fritz Barry Griffin
  • Start date
L

Lisa Fritz Barry Griffin

Hi there,

How can I do this in a one liner:

maxCountPerPhraseWordLength = {}
for i in range(1,MAX_PHRASES_LENGTH+1):
maxCountPerPhraseWordLength = 0

Thanks!
 
P

Peter Otten

Lisa said:
How can I do this in a one liner:

maxCountPerPhraseWordLength = {}
for i in range(1,MAX_PHRASES_LENGTH+1):
maxCountPerPhraseWordLength = 0


You can init a dictionary with zeroes with

d = dict.fromkeys(range(1, MAX_PHRASES_LENGTH+1), 0)

but this won't work as expected for mutable values:
d = dict.fromkeys(range(5), [])
d {0: [], 1: [], 2: [], 3: [], 4: []}
d[0].append(42)
d
{0: [42], 1: [42], 2: [42], 3: [42], 4: [42]}

Oops! all values refere to the same list.
Instead you can use a defaultdict which adds items as needed:
from collections import defaultdict
d = defaultdict(list)
d
d[0].append(42)
d
d[7].append(123)
d
defaultdict(<type 'list'>, {0: [42], 7: [123]})
 
V

Vlastimil Brom

2011/2/5 Lisa Fritz Barry Griffin said:
Hi there,

How can I do this in a one liner:

       maxCountPerPhraseWordLength = {}
       for i in range(1,MAX_PHRASES_LENGTH+1):
           maxCountPerPhraseWordLength = 0

Thanks!


Hi,
as has been already suggested, using collections.defaultdict may be
appropriate, but it's fairly straightforward to mimic the original
code with a generator expression in dict(...)
maxCountPerPhraseWordLength = dict((i,0) for i in range(1,MAX_PHRASES_LENGTH+1))
or with a dict literal (in newer python versions)
maxCountPerPhraseWordLength = {i:0 for i in range(1,MAX_PHRASES_LENGTH+1)}

(possibly using xrange for larger dicts - in pyton 2)

hth,
vbr
 
S

Steven D'Aprano

Hi there,

How can I do this in a one liner:

       maxCountPerPhraseWordLength = {}
       for i in range(1,MAX_PHRASES_LENGTH+1):
           maxCountPerPhraseWordLength = 0


maxCountPerPhraseWordLength = {0 for i in range(1,MAX_PHRASES_LENGTH+1)}


Unfortunately not. In versions before Python 2.7, it will give a
SyntaxError. In 2.7 or better it gives a set instead of a dict:
set([0])


This will do the job most simply:
dict(zip(range(1, MAX_PHRASES_LENGTH+1), [0]*MAX_PHRASES_LENGTH))
{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}


Or this:
{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}


But perhaps the most simple solution is to just leave the dict empty, and
instead of fetching items with dict, use dict.get(i, 0) or
dict.setdefault(i, 0).
 
D

Daniel Urban

Hi there,

How can I do this in a one liner:

       maxCountPerPhraseWordLength = {}
       for i in range(1,MAX_PHRASES_LENGTH+1):
           maxCountPerPhraseWordLength = 0


maxCountPerPhraseWordLength = {0 for i in range(1,MAX_PHRASES_LENGTH+1)}


Unfortunately not. In versions before Python 2.7, it will give a
SyntaxError. In 2.7 or better it gives a set instead of a dict:


Yeah, sorry, that should have been {i: 0 for i in
range(1,MAX_PHRASES_LENGTH+1)}


Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top