79273417

Date: 2024-12-11 23:11:06
Score: 0.5
Natty:
Report link

The prior solutions helped me a lot, but I also needed to change the value at the index of the tuple. For example: access(l, (0,0,0)) = 7.

The other solutions return a primitive value and so the nested list doesn't get updated.

I found that this approach, based on previous answers works (in case someone has the same need as me):

def set_it(obj, indexes, v):
   a = obj
   for i in indexes[:-1]:
     a = a[i]
   a[indexes[-1]] = v

Usage:

>>> l = [[[1, 2],
...       [3, 4]],
...      [[5, 6],
...       [7, 8]]]
>>> set_it(l, (0, 0, 0), -1)
>>> l
[[[-1, 2], [3, 4]], [[5, 6], [7, 8]]]
Reasons:
  • Blacklisted phrase (1): helped me a lot
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: thayne