I have a list where one of the elements is a variable and it can take a value between 60 to 120.
[0, 1, 2, 3, 4, v]
if I want to print this list for v ranges from 60 to 120:
for v in range(60,121):
print([0, 1, 2, 3, 4, v])
gives the expected result. However if I assign this list to a variable such as:
l = [0, 1, 2, 3, 4, v]
I get an error, because v
is undefined. If I try assigning an initial value to v
, then declare the list, I do not get an error anymore, but I get stuck with a list where v
has already assigned a value:
v = 60
l = [0, 1, 2, 3, 4, v]
for v in range(60,121):
print(l)
always yields [0, 1, 2, 3, 4, 60]
.
The reason I am trying to assign this list to a variable is, I use this list in many locations in a way longer script, and I need to do a for loop for certain calculations using this list for the range 60 to 120 for v
. One solution is typing the list every time it needs to be used, but that requires me to replace all occurrences of this list in the script if I need to change any other list element.
I think my question is: is there a way to assign a list that contains a variable (v
) as an element to a variable (l
) to be used throughout your script?
Is there a good programmatical way to handle something like this?
-
What you want is not possible. When you use a variable its value is copied, it's not a reference to the variable itself.Barmar– Barmar01/12/2021 06:31:06Commented Jan 12, 2021 at 6:31
-
Unless you use an element of a list or a dictionary.Countour-Integral– Countour-Integral01/12/2021 06:32:35Commented Jan 12, 2021 at 6:32
-
You are looking for a function. Lists, and anything really, don't contain variables. Variables are for source code. Your list contains objects.juanpa.arrivillaga– juanpa.arrivillaga01/12/2021 06:34:10Commented Jan 12, 2021 at 6:34
5 Answers 5
Define a function.
def mylist(x):
return [0, 1, 2, 3, 4, x]
for v in range(60, 121):
print(mylist(v))
You could define the list like this:
l = [0, 1, 2, 3, 4]
And introduce v
whenever you use it:
for v in range(60, 121):
print(l + [v])
print([1,2,3,4,*range(60,70)])
if you want to store as variable then
v= [*range(60,70)]
print([1,2,3,4,v])
if you don't want nested list then:
c=[1,2,3,4]
[c.append(i) for i in v ]
print(c)
Or
print([1,2,3,4,[i for i in v ]])
-
Check the 2nd version, it returns a nested listcostaparas– costaparas01/12/2021 06:43:37Commented Jan 12, 2021 at 6:43
-
1
-
print([1,2,3,4,[i for i in v ]]) even use thisPDHide– PDHide01/12/2021 06:48:00Commented Jan 12, 2021 at 6:48
Well it really depends on exactly how you're going to use this. If you want to evaluate something that uses this list at every possible value then
for v in range(60,121):
l = [0,1,2,3,4,v]
# code here
If you want to just assign a list with a certain value, just do something like this
def l(v):
return [0,1,2,3,4,v]
-
1No, do not do this:
l = lambda x: [0,1,2,3,4,x]
. That is against the official style guide. The only purpose for usinglambda
is to create an anonymous function, if you assign it to a name, that defeats the purposejuanpa.arrivillaga– juanpa.arrivillaga01/12/2021 06:36:06Commented Jan 12, 2021 at 6:36 -
I think its more pythonic to use lambda because it gets the point across faster instead of using the
def
syntax. In javascript for example, many people usefunc = () => {}
instead offunction func(){}
but hey, up to your personal perferencefeverdreme– feverdreme01/12/2021 06:38:39Commented Jan 12, 2021 at 6:38 -
You are wrong. It is explicitly against the official style guide, PEP8 to assign a lambda to a name. That is as close to an official definition of "pythonic" as exists. Python isn't Javascript, idiomatic Python code is not like idiomatic JS code. Idiomatic JS tends to be much more terse than idiomatic Python.juanpa.arrivillaga– juanpa.arrivillaga01/12/2021 06:40:33Commented Jan 12, 2021 at 6:40
-
Abuse of lambda. 2 minute timeout.tdelaney– tdelaney01/12/2021 06:40:58Commented Jan 12, 2021 at 6:40
-
1Well I think I just leatned something new, thanks. I'll edit my code so it follows the standardfeverdreme– feverdreme01/12/2021 06:48:31Commented Jan 12, 2021 at 6:48
Here is another solution... but I think its a bad one for anything but a small script. You could put a sentinel value in place of the anticipated v
. The problem is that if multiple bits of code use the list at the same time, one will overwrite the other's value, corrupting all. I'm including it because it most closely matches your original experiments.
oft_used_list = [0, 1, 2, 3, 4, None]
for v in range(60,121):
oft_used_list[-1] = v
print(oft_used_list)