Q:

Python Dictionary and Tuples Addition

Need a bit of help on this problem:
I have the following code:
def insertIntoDataStruct(state,job,count,dict):
    if not state in dict:
        print "adding"
        dict[state] = [(job,count)]
    else:
        for x in range(0, len(dict[state])):
            if(dict[state][x][0] == job):
                print "hi"
                print dict[state][x][0]
                print job
                print state
                print dict[state][x][1]
                dict[state][x][1] = dict[state][x][1] + 1
            else:
                dict[state].append((job,count))

courses = {}

insertIntoDataStruct("CA", "2121", (1), courses)
insertIntoDataStruct("CA", "169521", 1, courses)
insertIntoDataStruct("CA", "2121", 1, courses)
insertIntoDataStruct("TX", "2121", 1, courses)
insertIntoDataStruct("TX", "169521", 1, courses)
insertIntoDataStruct("TX", "262420", 1, courses)

print courses

and I am getting this error:
adding File "test2.py", line 21, in <module>
hi
insertIntoDataStruct("CA", "2121", 1, courses)
2121
File "test2.py", line 13, in insertIntoDataStruct
2121
dict[state][x][1] = dict[state][x][1] + 1
CA
TypeError: 'tuple' object does not support item assignment
1

Process finished with exit code 1

How can I go about fixing : TypeError: 'tuple' object does not support item assignment
The Ideal output of this code should be:
{
'CA': [('2121', 2), ('169521', 1), ('2122', 1)], 
'TX': [('2121', 1), ('169521', 1), ('262420', 1)]
}

Thanks for all help! 

A:

tuples are immutable. Therefore, when you try to run the line dict[state][x][1] = dict[state][x][1] + 1, where the left side is the tuple ('2121', 1), the 'tuple' object does not support item assignment error results.

