This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a list of variables in Python with below values.
I am trying to print the values of these variables in the following manner, but this is unsuccessful. Can someone please help with a solution ?
a1=1
a2=2
a3=3
for i in range(1,4):
temp="a"+str(i)
print(temp)
I want the output in 'temp' print the values(viz) 1,2,3 whereas the output seen are the variables (viz) a1,a2,a3
Try This:-
a1=1
a2=2
a3=3
for i in range(1,4):
temp="a"+str(i)
print locals()[temp]
Related
This question already has answers here:
Getting the name of a variable as a string
(32 answers)
Closed 1 year ago.
Consider the following code:
x,y = 0,1
for i in [x,y]:
print(i) # will print 0,1
Suppose I wanted instead to print:
x=0
y=1
I realise f-strings can be used to print the intermediate variable name:
for i in [x,y]:
print(f"{i=}") # will print i=0, i=1
However, I am interested in the actual variable name.
There are other workarounds: using eval or using zip([x,y], ['x', 'y']), but I was wondering if an alternative approach exists.
I think this achieves what you want to do -
for i in vars():
print(f'{i}={vars()[i]}')
This question already has answers here:
Removing entries from a dictionary based on values
(4 answers)
Closed 2 years ago.
for example:
a = {1:'hello' , 2:'bye'}
suppose I want to delete the element 2:'bye' through its value 'bye' , so can anyone help me by giving a piece of code.
thank you
for what I know you can do it like this
for i in a: # the dictionary
if a[i] == 'bye':
del a[i]
Hopefully this helps
This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 3 years ago.
I've a list of variable names, how can I print value of it using for loop..
var1="First"
var2="Second"
list=["var1","var2"]
for var in list:
print(var) # I want to print value of var,
present output
var1
var2
What I want is
First
Second
list=["var1","var2"]
You've made a list of strings, not variable names. What you need is
list=[var1,var2]
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 8 years ago.
x=1
code(x)=5
print(code1)
I want this to work andit print "5" could anybody help me do this in python.
myList=[1,2,3,4,5,6]
print myList[2]
will print 3. You can assign an index to a variable and do this:
index=2
print myList[index]
will print 3.
You can use "code" as a list.
x=1
code=[0,0]
code[x]=5
print(code[1])
or as a dict:
x=1
code={}
code[x]=5
print(code[1])
depends on how you will use "code".
As the other answer suggested, you'll better use dictionary or a list for this.
But for your question you could it like this:
x = 1
exec(('code' + x) + '=5')
print code1
code1 will be 5.
code=[0,0]
x=1
code[x]=5
print(code[1])
This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.