I have a list of items they may or may not be arbitrarily nested. I would like to replace one of the lists' elements. Either way (nested or not), I have the element's index location stored in another list.
Here's a nested list example where I would like to replace 'xyz' with something else, say 123. I have the location of 'xyz' stored in loc:
find='xyz'
replace=123
nested=[['abc',1],['xyz',2]]
print(loc) # [1,0]
Using loc how can I substitute 'xyz' for 123?
Here is an unnested example where I would like to do the same substitution:
unnested=['abc','xyz']
print(loc) # [1]
If loc only has one element then you can simply do:
*nest,element=loc
if not nest:
unnested[element]=replace
else: pass # need help with this part
Is there something flexible enough to handle both cases?
loc. I will edit this to make it a list of lists.