Skip to content Skip to sidebar Skip to footer

Edit Integers In A List Python

Recently I have been working on some Python coding, and ran into a small problem. Would it be possible to edit values in a list, like add one to an existing number, or do I have to

Solution 1:

What you have is probably the easiest way in doing so, so use it :D. You aren't removing a value there, but you're changing the value.

Also, X[2] = X[2] + 1 can be simplified to X[2] += 1 (it's the exact same).

Solution 2:

Lists are mutable in Python - See here. So you can add, remove, and edit values in place.

So your code would work as is. The shortcut in Python for adding to existing values is:

>>>x = [1, 2, 3]>>>x[2] += 1>>>x
[1,2,4]

Post a Comment for "Edit Integers In A List Python"