Skip to content
Advertisement

Changing the value of tuple inside a nested list

How do I change the second value of the first tuple in the first list by force??

JavaScript

Please help! Thanks in advance.

Advertisement

Answer

Tuples are immutable. You would need to replace it instead.

Since lists are mutable, you would replace the first element of the first list with a new tuple having a changed second value.

JavaScript

This would give you:

JavaScript

To do it in one line:

JavaScript

Note that if you were using named tuples, you could simplify this slightly with a ._replace() method (https://docs.python.org/2/library/collections.html#collections.somenamedtuple._replace):

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement