What you should probably do is just return 2
and then use that as an index into the list you passed in:
lyst = [7, 8, 9, 10]
def func(lst):
# ...
return 2
lyst[func(lyst)] = 11
You can create an object that encapsulates a reference to the container plus the index (or key, in the case of a dictionary) of the particular element:
class ElementRef(object):
def __init__(self, obj, key):
self.obj = obj
self.key = key
@property
def value(self): return self.obj[self.key]
@value.setter
def value(self, value): self.obj[self.key] = value
Then your function looks like this:
def func(lyst):
# ...
return ElementRef(lyst, 2)
And you can modify the element by setting its value
attribute like this:
func(lyst).value = 11
But I don't see a lot of value in this over just returning the index. Especially because the object can become useless if items are added to or removed from the list before it's used. Explicitly returning an index makes this an obvious problem, but if you get back an ElementRef
you may assume it contains some kind of magic that it doesn't.