I want to use a variable to call a global list with the same name. So, for example:
x = [1,2,3,4]
y = [2,3,4,5]
def function(i):
return i
function(x)
expected output -> [1,2,3,4]
I want to use a variable to call a global list with the same name. So, for example:
x = [1,2,3,4]
y = [2,3,4,5]
def function(i):
return i
function(x)
expected output -> [1,2,3,4]
I assume you mean that
>>> x = [1,2,3,4]
>>> x == function('x')
True
where passing the string 'x'
to the function returns the list assigned to x
.
You don't want to write code that depends on the names of variables; instead, keep a dictionary that maps strings to your lists.
>>> d = dict(x=[1, 2, 3, 4], y=[2, 3, 4, 5])
>>> d['x']
[1, 2, 3, 4]