?
Solved

Using setdefault to update that value of an int

Posted on 2006-07-23
4
Medium Priority
?
249 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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…

762 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