views:

87

answers:

3

In Python, I need a dictionary object which looks like:

{'a': 10, 'b': 20, 'c': 10, 'd': 10, 'e': 20}

I've been able to get this successfully by combining the dict.update() and dict.fromkeys() functions like so:

myDict = {}
myDict.update(dict.fromkeys(['a', 'b', 'c'], 10))
myDict.update(dict.fromkeys(['b', 'e'], 20))

However, because the code is being written for novice users who may need to make add keys/values on occasion, I'd prefer a simple bare-bones (Perl-like) syntax such as:

myDict = {}
myDict['a', 'c', 'd'] = 10
myDict['b', 'e'] = 20

This, however, gives me:

myDict = {('a', 'c', 'd'): 10, ('b', 'e'): 20}

Is there a way I can simplify my first example (using dict.update() and dict.fromkeys()) further, and get the dict object I'm looking for?

Or, alternatively, if I have a dict with tuples as in my second example, is there an easy way for me to do a lookup such as myDict['c'] or myDict.get('c') and get the value 10?

+2  A: 

I would say what you have is very simple, you could slightly improve it to be:

my_dict = dict.fromkeys(['a', 'b', 'c'], 10)
my_dict.update(dict.fromkeys(['b', 'e'], 20))

If your keys are tuple you could do:

>>> my_dict = {('a', 'c', 'd'): 10, ('b', 'e'): 20}
>>> next(v for k, v in my_dict.items() if 'c' in k)      # use .iteritems() python-2.x
10

This is, of course, will return first encountered value, key for which contains given element.

SilentGhost
The OP said the code is being written for novices, and this is relatively difficult to comprehend for a novice.
taleinat
@teleinat: so write a function that they could use. Are you seriously downovoting me because my code is not stupid enough? My code is using built-in methods and function. How is that not basics? What is the point if writing code for novice if they're not going to learn from it?
SilentGhost
My rule of thumb is to never downvote **valid** answers.
Xavier Ho
The OP obviously knows how to program in Python. I interpret his question as whether there is a simpler built-in solution for his use-case, not whether it is possible to write such a function or achieve what he wanted with complex code. Therefore, as I understand it SilentGhost's response doesn't answer the question, so I downvoted it. My initial comment didn't describe my reason for downvoting.
taleinat
@taleinat: *Or, alternatively, if I have a dict with tuples as in my second example, is there an easy way for me to do a lookup such as `myDict['c']` or `myDict.get('c')` and get the value `10`?* are we not reading the same question? Proposed solution is the only *easy* way to do this.
SilentGhost
@SilentGhost: IMO the OP knows how to program and could have conjured up something equivalent himself; he was asking if there was an existing, simple mechanism for this, e.g. built-in or in the standard library.
taleinat
@taleinat: surely if OP *knows how to program*, he knows that there isn't anything built-in of sorts.
SilentGhost
@SilentGhost I disagree. I've been programming in Python for nine years and still once in a while I learn of new functionality in the standard library. Knowing to use a language and knowing its entire standard library inside and out are very different things.I don't think either of us is going to be conviced, however, so let's just agree to disagree.
taleinat
BillyBBone
+2  A: 

Your first example can be simplified using a loop:

myDict = {}
for key in ['a', 'c', 'd']:
    myDict[key] = 10
for key in ['b', 'e']:
    myDict[key] = 20

No specialized syntax or trickery, and I can't think of anything which would be easier to understand.

Regarding your second question, there is no simple and efficient way to do the lookup as in your second example. I can only think of iterating over the keys (tuples) and checking whether the key is in any of them, which isn't what you're looking for. Stick to using a straightforward dict with the keys you want.

In general, if you are aiming for code that can be understood by novices, stick to the basics such as if conditions and for/while loops.

taleinat
A: 

Why not inherit from dict?

class LazyDict(dict):
    def keylist(self, keys, value):
        for key in keys:
            self[key] = value

>>> d = LazyDict()
>>> d.keylist(('a', 'b', 'c'), 10)
>>> d
{'a': 10, 'c': 10, 'b': 10}

but I prefer loop solution

remosu