Python Forum

Full Version: Editing Dictionaries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 3 dictionaries in which I am trying to update the values of the keys.
The keys of each dictionary are identical, so I thought I could simply cycle round the dictionaries using a 'For' condition.

A = {'Bi' : 0,'Of' : 0,'Spd' : 0}
E = {'Bi' : 0,'Of' : 0,'Spd' : 0}
U = {'Bi' : 0,'Of' : 0,'Spd' : 0}

print(A)
Ab = 24
Ao = 41
Eb = 67
Eo = 80
Ub = 15
Uo = 30


for i in range(3): 
        if i == 0:
                Pair = 'A'
        elif i == 1:
                Pair = 'E'
        elif i == 2:
                Pair = 'U'
        
        Pair['Bi'] = Pair + 'b'
        Pair['Of'] = Pair + 'o'
        Pair['Spd'] = Pair + 'o' - Pair + 'b'
        print(Pair)
However, when run I get only the first printout followed by an error message as follows:

"{'Bi': 0, 'Of': 0, 'Spd': 0}
Traceback (most recent call last):
File "C:\Users\Astrikor\Desktop\CyclePairs.py", line 23, in <module>
Pair['Bi'] = Pair + 'b'
TypeError: 'str' object does not support item assignment"

As I am still a (very) newbie, I am sure I have a misunderstanding somewhere here (maybe I should be using a Function ?)

Any help would be welcome

Many thanks
Astrikor
                Pair = 'A'
        elif i == 1:
                Pair = 'E'
        elif i == 2:
                Pair = 'U'
Here you assign a string to Pair and not the dictionary you want to work with. This means your code really does:
'A'['Bi'] = Pair + 'b'  # when it should be A['Bi'] = Pair + 'b'
So instead just remove the parentheses from A E and U.
To add to what jcrater said, you can totally simplify your loop:

for Pair in (A, E, U):
    Pair['Bi'] = Pair + 'b'
    ...
Of course, you will get another error on Pair['Bi'] = Pair + 'b', because you are trying to add a dictionary to a string, which won't work. But it's not clear to me what you're expecting to happen there, so I'm not sure how to fix it.
Thanks for the replies,

By way of explanation, I was hoping to allocate the values of the 6 variables to the values of the dictionary keys, such that

when Pair = A then

Pair['Bi'] = Pair + 'b'

becomes:

A['Bi'] = 'Ab' (i.e A['Bi'] = 24)

and

A['Spd'] = float(Ao) - float(Ab) (i.e A['Spd'] = 41 - 24 = 17)

The purpose being to assign the respective values to each dictionary to result as follows:

A = {'Bi' : 24,'Of' : 41,'Spd' : 17}
E = {'Bi' : 67,'Of' : 80,'Spd' : 13}
U = {'Bi' : 15,'Of' : 30,'Spd' : 15}

and I was expecting the final print(Pair) to print out each dictionary in turn with the new values inserted.

(Subsequently new values of the 6 variables will then need to be inserted as new data is collected.)

But maybe there is a better way to do this?
Well, if you put all of your pair dictionaries in another dictionary:

Pairs = {'A': A, 'E': E, 'U': U}
Then you could do:

key = 'A'
Pairs[key]['Bi'] = key + 'b'
Then Pairs['A']['Bi'] would equal 'Ab'.
Thanks Guys,
I am slowly learning!
I have removed the parentheses (j.crater), and also adopted ichabod801's suggestion to combine the three dictionaries.

A = {'Bi' : 0,'Of' : 0,'Spd' : 0}
E = {'Bi' : 0,'Of' : 0,'Spd' : 0}
U = {'Bi' : 0,'Of' : 0,'Spd' : 0}


Ab = 24
Ao = 41
Eb = 67
Eo = 80
Ub = 15
Uo = 30


Pairs = {'A': A, 'E': E, 'U': U}
print(Pairs)

for key in Pairs:
    Pairs[key]['Bi'] = key + 'b'
    Pairs[key]['Of'] = key + 'o'
    
print(Pairs)
It's concise code and a very nice improvement on the original.
The printout is as follows:

"{'A': {'Bi': 0, 'Of': 0, 'Spd': 0}, 'E': {'Bi': 0, 'Of': 0, 'Spd': 0}, 'U': {'Bi': 0, 'Of': 0, 'Spd': 0}}
{'A': {'Bi': 'Ab', 'Of': 'Ao', 'Spd': 0}, 'E': {'Bi': 'Eb', 'Of': 'Eo', 'Spd': 0}, 'U': {'Bi': 'Ub', 'Of': 'Uo', 'Spd': 0}}"

Which whilst it runs correctly, it does not load the values of the variables
e.g instead of 'Ab' I need it to load the value of Ab (i.e 24) for the key 'Bi' etc...

If I try replacing
Pairs[key]['Bi'] = key + 'b'
with
Pairs[key]['Bi'] = float(key + 'b')
this error shows up:

ValueError: could not convert string to float: 'Ab'

I have searched on line for this error, but can't see how apply a correction.

Any further suggestions?

Sorry to test your patience....

Astrikor
Again, dictionaries:

Pairs = {'A': {'Bi' : 0,'Of' : 0,'Spd' : 0}}
Pairs['E'] = {'Bi' : 0,'Of' : 0,'Spd' : 0}
Pairs['U'] = {'Bi' : 0,'Of' : 0,'Spd' : 0}

Values = {'Ab': 24, 'Ao': 41, 'Eb': 67, 'Eo': 80, 'Ub': 15, 'Uo': 30}

print(Pairs)

for key in Pairs:
    Pairs[key]['Bi'] = Values[key + 'b']
    Pairs[key]['Of'] = Values[key + 'o']

print(Pairs)
Output:
{'A': {'Of': 0, 'Spd': 0, 'Bi': 0}, 'U': {'Of': 0, 'Spd': 0, 'Bi': 0}, 'E': {'Of': 0, 'Spd': 0, 'Bi': 0}} {'A': {'Of': 41, 'Spd': 0, 'Bi': 24}, 'U': {'Of': 30, 'Spd': 0, 'Bi': 15}, 'E': {'Of': 80, 'Spd': 0, 'Bi': 67}}
That's sorted then!

So elegant when you know.

Many thanks Ichabod801.
Astrikor