0

I am initializing a dictionary of empty lists. Is there a more efficient method of initializing the dictionary than the following?

dictLists = {}
dictLists['xcg'] = []
dictLists['bsd'] = []
dictLists['ghf'] = []
dictLists['cda'] = []
...

Is there a way I do not have to write dictLists each time, or is this unavoidable?

4
  • Are the dictionary keys what you actually want? Does it matter if they are random or an arithmetic progression? Commented Jun 1, 2012 at 14:42
  • Loads of answers guessing your keys are stored in a variable or similar and you are looping. Are your keys static like that? Or are they generated? Commented Jun 1, 2012 at 14:47
  • @MartijnPieters All (most?) of the answers utilizing keys stored in an iterable can be made static by just replacing the variable name with a list or tuple literal. Using a variable just makes the representation more elegant. Commented Jun 1, 2012 at 16:48
  • @JAB: Have you seen my answer, below? :-) It actually uses static list in a variable in one example. Contrast this with generating the keys (keys = ['key%02i' for i in range(10)]). Commented Jun 1, 2012 at 16:52

5 Answers 5

8

You can use collections.defaultdict it allows you to set a factory method that returns specific values on missing keys.

a = collections.defaultdict(list)

Edit:

Here are my keys

b = ['a', 'b','c','d','e']

Now here is me using the "predefined keys"

for key in b:
    a[key].append(33) 
Sign up to request clarification or add additional context in comments.

Comments

7

If the keys are known in advance, you can do

dictLists = dict((key, []) for key in ["xcg", "bsd", ...])

Or, in Python >=2.7:

dictLists = {key: [] for key in ["xcg", "bsd", ...]}

Comments

4

If you adding static keys, why not just directly put them in the dict constructor?

dictLists = dict(xcg=[], bsd=[], ghf=[])

or, if your keys are not always also valid python identifiers:

dictLists = {'xcg': [], 'bsd': [], 'ghf': []}

If, on the other hand, your keys are not static or easily stored in a sequence, looping over the keys could be more efficient if there are a lot of them. The following example still types out the keys variable making this no more efficient than the above methods, but if keys were generated in some way this could be more efficient:

keys = ['xcg', 'bsd', 'ghf', …]
dictLists = {key: [] for key in keys}

1 Comment

This is definitely most efficient, as the poster asked.
2

You can also use a for loop for the keys to add:

for i in ("xcg", "bsd", "ghf", "cda"):
    dictList[i] = []

Comments

1

In the versions of Python that support dictionary comprehensions (Python 2.7+):

dictLists = {listKey: list() for listKey in listKeys}

1 Comment

@MartijnPieters Thanks, simplified my answer now that I have the confirmation. [] could also work in place of list(), as well, as used in someone else's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.