3

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?

asked Jan 12, 2021 at 6:29
3
  • What you want is not possible. When you use a variable its value is copied, it's not a reference to the variable itself. Commented Jan 12, 2021 at 6:31
  • Unless you use an element of a list or a dictionary. Commented 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. Commented Jan 12, 2021 at 6:34

5 Answers 5

2

Define a function.

def mylist(x):
 return [0, 1, 2, 3, 4, x]
for v in range(60, 121):
 print(mylist(v))
answered Jan 12, 2021 at 6:33
1

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])
answered Jan 12, 2021 at 6:34
1
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 ]]) 
answered Jan 12, 2021 at 6:39
3
  • Check the 2nd version, it returns a nested list Commented Jan 12, 2021 at 6:43
  • 1
    @costaparas added for non nested Commented Jan 12, 2021 at 6:45
  • print([1,2,3,4,[i for i in v ]]) even use this Commented Jan 12, 2021 at 6:48
1

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]
answered Jan 12, 2021 at 6:35
5
  • 1
    No, do not do this: l = lambda x: [0,1,2,3,4,x]. That is against the official style guide. The only purpose for using lambda is to create an anonymous function, if you assign it to a name, that defeats the purpose Commented 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 use func = () => {} instead of function func(){} but hey, up to your personal perference Commented 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. Commented Jan 12, 2021 at 6:40
  • Abuse of lambda. 2 minute timeout. Commented Jan 12, 2021 at 6:40
  • 1
    Well I think I just leatned something new, thanks. I'll edit my code so it follows the standard Commented Jan 12, 2021 at 6:48
0

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)
answered Jan 12, 2021 at 6:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.