if string contains global variable, how to use that global variable? [duplicate] - python

This question already has answers here:
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I couldn't find an answer for this, so can you give a beginner pythonist some help. I have a string and if it is in globals, then I want to use that variable to manipulate it. I get an error that I'm using string to append, when my global variable is a list.
color = []
keyword = "color"
if keyword in globals():
keyword.append("testing")

globals() returns a dictionary, which you can use like any other dictionary
globals()[keyword] = <my_new_value>

Related

Can we use a variable to create a new variable in Python? [duplicate]

This question already has answers here:
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 5 months ago.
I am wondering if we can use a variable name in order to create a new variable, for example:
Let's assume I have this variable:
Var = 'Jim'
Lets say I want to concatenate the variable with a string, in this case the string is the word Mr:
NewVar = "String"Var
So that if I print the new variable, the output would look something like:
MrJim
This can be achieved in bash like this:
NewVar=Mr${Var}
But I have not found a way to do this in Python. Please let me know if you know how to do it.
Have a look at python string interpolation.
var = "Jim"
new_var = f"Mr {var}"

How to look for a variable named the same as a string in Python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
#Now I would like to somehow get the list printed or returned by only using the string variable.
Use globals or locals:
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
print(globals()[string])
['a','b','c']
If you have multiple list and you want to print them based on some key it's best to use a dictionary.
lstDic = {
"x":['a','b','c'],
"y":['d','e','f'],
"y":['g','h','i']
}
string='x'
print(lstDic[string])

Python Passing list by its name to a function [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 8 months ago.
names=['abcd','efgh']
nameoflist='names'
def(nameoflist=[]):
return nameoflist
I want to be able to return the entire list from the function
Assuming names is global as specified in the question, you can do this
names=['abcd','efgh']
nameoflist='names'
def return_names(nameoflist):
return globals()[nameoflist]
However, this is pretty ugly, and I'd probably try another way to do it. What do you need the name for? Is there any other way to get the information you're asking for?
This one way to do what you are asking. But it is not good programming.
names=['abcd','efgh']
def list_by_name(list_name):
return eval(list_name)
print(list_by_name('names'))
Also, argument list_name should be a string. It should not default to a list, which would make the function to fail if called without argument. It should not have a default value.

Get local name of variable issued to function [duplicate]

This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 5 years ago.
want to get name of variable issued from outside.
For example:
def process(var):
print name(var)#prints 'nice_name'
nice_name=1
process(nice_name)
Is it even possible in python?
No, as we are just passing a value to the function

set a key without value for a dictionary in python [duplicate]

This question already has answers here:
Python function global variables? [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to set a dictionary for the staff's salary in Python. I want to create two functions, getname() and getsalary() so I tried:
empDic={}
def setname(n):
empDic={'n':''}
And then in python interactive 2.7 I typed:
>>>setname('marson')
>>>print empDic
{}
The result is still an empty dictionary, How can I deal with that?
You would need to modify the original dict:
empDic={}
def setname(d, n):
d[n] = ""
setname(empDic,"marson")
print(empDic)
{'marson': ''}
You are creating a new local variable empDic in your function, you are also using a string "n" not the name n passed in to the function.

Categories

Resources