[Tutor] Is this correct syntax for what I want?
Alan Gauld
alan.gauld at freenet.co.uk
Sat Jun 24 23:54:31 CEST 2006
> The data structure is:
> mydata = [(Checking, 12.50),(Savings, 34.50)]
>
> And I want the result to look like this:
> mydata = [(Checking, 19.50),(Savings, 34.50)]
>
> So how do I do this?
OK, The problem is that you cannot change the contents of a
tuple, you can only create a new tuple. Therefore you are
probably better changing to a list or dictionary.
>From the look of the data I'd suggest a dictionary might
be best:
myData = { 'Checking': 12.50, 'Savings': 34.50}
Then you can do:
myData['Checking'] += 7.00
If you decide to go for a list then it would look like:
myData = [['Checking', 12.5],['Savings',34.50]]
And you would modify it with:
myData[0][1] += 7 # [0][1] = second element of first list
But I think you will agree the dictionary approach looks
a lot nicer, so unless there is a good reason why you
can't use that I'd strongly recommend a dictionary.
HTH,
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list