Adding an entry to a python tuple

I have a list of tuples representing the points x, y. I also have a list of values ​​for each of these points. How to combine them into a list of lists (for example, one entry for each point [x, y, val]) or a list of tuples?

thank

+3
source share
2 answers

You cannot add records to tuples, because tuples are immutable. But you can create a new list of lists:

new = [[x, y, val] for (x, y), val in zip(points, vals)]
+10
source

Tuples are immutable; they cannot be changed. Convert it to a list, and then convert it back if you want (list ((a, b))).

+1
source

Source: https://habr.com/ru/post/1733226/


All Articles