Solved

Using setdefault to update that value of an int

Posted on 2006-07-23
4
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

688 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