?
Solved

Using setdefault to update that value of an int

Posted on 2006-07-23
4
Medium Priority
?
257 Views
Last Modified: 2010-04-16
I've recently been introducted to the setdefault function, and understand it can be used in the following way to update a dict's value, when that value is an array:

myDict.setdefault(myKey, []).append(myValue)

But how can I use setdefault to update the value of a dict, when that value is an int? For instance, the following statement gives me an error mesage:

myDict.setdefault(myKey, 0) += 1


Thanks!
0
Comment
Question by:BerkeleyJeff
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 2000 total points
ID: 17165847
That idiom only really works with mutable values.  With immutable values like integers, there's no way to avoid a separate get and set.  Here's one way to do it:

myDict[myKey] = myDict.get(myKey, 0) + 1

You can always break it out into a function:

def inc(d, k):
    d[k] = d.get(k, 0) + 1

myDict = {}
myKey = 'k'
inc(myDict, myKey)
print myDict
0
 

Author Comment

by:BerkeleyJeff
ID: 17165884
It's too bad Python doesn't have a builtin function for that. It's such a common task, I would think that would have included it.

Is there a mutable int type, like what "Integer" is to "int" in Java? Maybe I could halve the number of dict accesses that way.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 17166275
No, there's no mutable integer type.  You could write one, but it wouldn't make any appreciable difference - rather than two dictionary accesses, you'd have one dictionary access plus an object attribute access (which is a dictionary access under the hood anyway).
0
 

Author Comment

by:BerkeleyJeff
ID: 17168651
Thank you.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question