Solved

Using setdefault to update that value of an int

Posted on 2006-07-23
4
241 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 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

830 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