Integers are immutable. That means that once you've created an integer object, e.g. with a = 1
, it can never be changed. That doesn't mean that you can't rebind the name a
to another object, just that the value of the integer object itself won't change.
When you do a = a + 1
, you are creating a new integer with value 2, and assigning it to a
. But the original 1 is still around. In fact the element my_list[0]
still refers to the original object, since it was never told to refer to anything else. In that sense, names are like pointers in Python.
To get the list element to change, you have two options:
Tell it to change explicitly. Any time you modify a
, set my_list[0] = a
again. Or just drop a
entirely. Why have two references to the same object of you don't need to? You can do
my_list[0] += 1
my_list[1] *= 2
Store a mutable object in the list. Strings and integers, as you already saw, are immutable. Tuples are as well. But sets, dicts and lists are mutable, so any in-place changes you make to them will be directly visible. This is probably not the best option in your particular case, but here's an example:
a = [1]
b = {2}
my_list = [a, b]
a[0] += 1
a.append(3)
b.add(4)
print my_list
[[2, 3], {2, 4}]
Both of methods shown here work just fine for lists. The notation for sets is a little different for sets since in the first case, you'd have to remove the original object and insert the new one. The concept is the same though.
Only the second method could work with tuples though, and even that's not usually recommended. As I mentioned earlier, tuples are supposed to be immutable. The are certain expectations that come with that, like hashability, which are violated when you put a mutable object, like a list, into a tuple. It's perfectly fine to do that, but you just have to be a little careful.
For your particular use-case (accessing the values by name, but also grouping them into a single data structure), dictionaries may be more suitable than lists. You can index a dictionary much like a list, except that the keys can be the names you want instead of essentially arbitrary indices. Your example would look like this:
my_dict = {
'a': 1,
'b': 2,
}
my_dict['a'] += 1
my_dict['b'] *= 2
print my_dict
<p/>
{'a': 2, 'b': 4}