19

I'm an amateur when it comes to programming, but I'm trying my hand at Python. Basically what I want to be able to do is use math on a dictionary value. The only way I could think to do it would be to assign a dictionary value to a variable, then assign the new value to the dictionary. Something like this:

my_dictionary {
    'foo' = 10,
    'bar' = 20,
    }

variable = my_dictionary['foo']
new_variable += variable
my_dictionary['foo'] = new_variable

However, when I try to assign a variable this way, I get a syntax error. Is there a better and straightforward way to do this?

EDIT: Also, I am only using this as an example. Let's say this is a block of code, not the entire program. The fact that new variable has not been declared is completely irrelevant, I just want to know how to perform math to a dictionary value. Also, I am attempting to access a dictionary outside of the function I have my code in.

1
  • 2
    This isn't entirely clear. Where did new_variable come from? If you're trying to do update assignment to a value in a dictionary just do my_dict['foo'] += 1 or whatever. Commented Nov 16, 2012 at 23:44

7 Answers 7

21

There are various mistakes in your code. First you forgot the = in the first line. Additionally in a dict definition you have to use : to separate the keys from the values.

Next thing is that you have to define new_variable first before you can add something to it.

This will work:

my_dictionary = {'foo' : 10, 'bar' : 20}

variable = my_dictionary['foo']
new_variable = 0 # Get the value from another place
new_variable += variable
my_dictionary['foo'] = new_variable

But you could simply add new_variable directly to the dict entry:

my_dictionary = {'foo' : 10, 'bar' : 20}

variable = my_dictionary['foo']
new_variable = 0 # Get the value from another place
my_dictionary['foo'] += new_variable
Sign up to request clarification or add additional context in comments.

Comments

2

Try This. It worked for Me.

>>> d = {'a': 1, 'b': 2}
>>> locals().update(d)
>>> print(a)
1

Comments

1

You are trying to operate on a variable that doesn't exist

new_variable += variable

new_variable isn't defined, so you can't increment it. Simply try.

my_dictionary['foo'] += my_dictionary['foo']

I'm assuming the my_dictionary is just pseudocode, and not actually the definition you're using in your code, otherwise that is invalid too.

Comments

0

This code doesn't assign the variable my_dictionary. Try:

my_dictionary = {
    'foo'...

Also, you need to use colons for key-value initialization in dict. For example: 'foo' : 10

EDIT: forgot to mention, if you use += to change a variable, the variable must exist beforehand.

Comments

0

You're getting a SyntaxError when you define the dictionary because you're doing it all wrong. Try looking at another Python program (or documentation) and doing it the way it is shown there, rather than just winging it.

my_dictionary = {'foo': 10, 'bar': 20}

The math part is fine syntactically, but you are making it far harder than you need to:

my_dictionary['foo'] += whatever

Comments

0

That's not how you create a dictionary. This is the correct syntax:

my_dictionary = {
    'foo': 10,
    'bar': 20,
}

The final comma is unnecessary, but makes subsequent editing less error-prone. You can also do it this way:

my_dictionary = dict(foo=10, bar=20)

You can use the += operator to update a dictionary value in place:

my_dictionary['foo'] += 30

Comments

0

I like the following way because you can match a dictionary key with a variable name. This example shows how you can match a char with a corresponding variable name (and that variable name is even dictated by other variables):

va1 = 50
val2= 20

a = val + 1 * val2
b = val + 2 * val2
space = val + 3 * val2

my_dictionary = {
'a':a,
'b':b,
' ':space
}

msg = "aB "
for char in msg:
    try:
        char = char.lower()
        print my_dictionary [char]
    except:
        print my_dictionary [char]

Output:

70

90

110

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.