473,411 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to update value in dictionary?

dict.update({"a":1}) SETS the dict item "a" 's value to 1.

i want to increase it by 1. isnt that possible in an easy way? I
should use a tuple for this?
Aug 27 '08 #1
6 72181
On Aug 27, 2:40 pm, ssecorp <circularf...@gmail.comwrote:
dict.update({"a":1}) SETS the dict item "a" 's value to 1.

i want to increase it by 1. isnt that possible in an easy way? I
should use a tuple for this?
dict["a"] += 1

Iain
Aug 27 '08 #2
ssecorp wrote:
dict.update({"a":1}) SETS the dict item "a" 's value to 1.

i want to increase it by 1. isnt that possible in an easy way? I
should use a tuple for this?

1) Don't use dict as name for a dictionary, it shadows the type dict

2) setdefault is your friend

d = {}
d['a'] = d.setdefault('a', 1) + 1

Diez
Aug 27 '08 #3
ssecorp wrote:
dict.update({"a":1}) SETS the dict item "a" 's value to 1.
That works but it's not the right way to do that, use this:

d["a"] = 1

Bye,
bearophile
Aug 27 '08 #4
Am Wed, 27 Aug 2008 15:45:13 +0200 schrieb Diez B. Roggisch:
>
>dict.update({"a":1}) SETS the dict item "a" 's value to 1.

i want to increase it by 1. isnt that possible in an easy way?
I should use a tuple for this?


1) Don't use dict as name for a dictionary, it shadows the type dict

2) setdefault is your friend

d = {}
d['a'] = d.setdefault('a', 1) + 1
d['a'] = d.get('a', 1) + 1

seems to me a little better, as d['a'] doesn't get set twice, right?

Curious
Martin

Aug 27 '08 #5
mblume wrote:
>2) setdefault is your friend

d = {}
d['a'] = d.setdefault('a', 1) + 1
d['a'] = d.get('a', 1) + 1

seems to me a little better, as d['a'] doesn't get set twice, right?
setdefault is pronounced "get, and set if necessary". it only updates
the dictionary if the key isn't already there:
>>help({}.setdefault)
Help on built-in function setdefault:

setdefault(...)
D.setdefault(k[,d]) -D.get(k,d), also set D[k]=d if k not in D

(since setdefault is a method, the second argument is evaluated whether
it's used or not, but that's true for your approach as well, and isn't
much of a problem for a light-weight object like the integer 1.)

</F>

Aug 27 '08 #6
On Aug 27, 8:01*pm, Fredrik Lundh <fred...@pythonware.comwrote:
mblume wrote:
2) setdefault is your friend
d = {}
d['a'] = d.setdefault('a', 1) + 1
d['a'] = d.get('a', 1) + 1
seems to me a little better, as d['a'] doesn't get set twice, right?

setdefault is pronounced "get, and set if necessary". *it only updates
the dictionary if the key isn't already there:

*>>help({}.setdefault)
Help on built-in function setdefault:

setdefault(...)
* * *D.setdefault(k[,d]) -D.get(k,d), also set D[k]=d if k not in D

(since setdefault is a method, the second argument is evaluated whether
it's used or not, but that's true for your approach as well, and isn't
much of a problem for a light-weight object like the integer 1.)
Both

d['a'] = d.setdefault('a', 1) + 1

and

d['a'] = d.get('a', 1) + 1

will set d['a'] to 2 if 'a' isn't initially in d.
Aug 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
1
by: esperanza | last post by:
Hello ASPX expert !! Here is my problem. I have a datagrid binded to a dataview. I edit the datagrid and change the value of my textBoxes from my datagrid. When I click to update the data,
2
by: Jose | last post by:
I have a combobox with two possible values: ONE TWO when i was pushed my save button save all values of my forms but this value ONE or TWO don't my code is:...
1
by: vdama | last post by:
Hi, I am using MS-Access as back end table for VB application. In the access table there are four columns as sno,productioncount ,percentage and totalcount. Here in prodcution count filed we have...
1
by: HarryW | last post by:
Hio, I want to update teh database Slim98 with some values out of database Slim0. The databasestructure is equal. update Slim98.dbo.article set Slim98.dbo.article.insuranceinventory =...
7
by: Sharma Neha | last post by:
How can i update the values in a table in database.The condition to update is a value in a textbox. I hav a textbox containing the order value.The table order_detail should be updated for its some...
1
by: amit1990 | last post by:
sir i use a datagrid view control in vb.net 2005 to display price of products and discount on those particular products so while i type the discount of those products i need to calculate the price...
1
by: Bigdaddrock | last post by:
I have a form, containing three text boxes: ZIP, ZIPCODE, and TAXCODE. The first is a nine digit zip code. Using a SetValue Macro, I am able to place a value in ZipCode from Zip via the following...
1
by: Mena Williams | last post by:
I have an order table and an inventory table. I have a form with combo and text boxes to add new orders to order table. I want to be able to reduce the # of stock in inventory table by number of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.