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.
Related
This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.
You can check that using the in keyword -
if object1 in object2:
#do something
Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something
This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 2 years ago.
Taking an intro to python course, the following code sorts the string words appropriately, but the function does not return sortString, so I'm not understanding why the output is correct. Can you please help me understand?
def sort_words(string):
splitString = string.split()
sortString = splitString.sort()
return splitString
print(sort_words('python is pretty cool'))
Python .sort() returns None, much like print() does. list.sort() works in place - meaning the list is sorted without needing to assign it to another variable name.
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>
This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 years ago.
Sorry for the title but I'm not sure how else to word it. Anyway I'm just starting to learn Python and ran into this problem. I'm trying to assign a variable to a function call, where the function contains input()
Unfortunately this never assigns anything to the variable a
def question(letter):
input(letter + '? ')
a = question('a')
print(a)
So I guess my real question is, why doesn't that work? Why doesn't it assign the user input to the variable a?
Thanks
You need to return the user input. Right now the user input is captured nowhere.
def question(letter):
return input(letter + '? ')
a = question('a')
print(a)
This question already has answers here:
Converting String into Object Python
(2 answers)
Closed 9 years ago.
Recently, I have been programming in the language of Python. I've came across the problem of trying to convert a string into a keyword.
Suppose I use a raw_input() phrase and turn that string into an object, list, or dictionary.
For example, I can turn the string "Foo" into Foo and assign that name to a python structure.
How would I do this?
You don't want to do that, so instead of doing it, use a dictionary:
answer = raw_input("Enter: ") # Let's assume I enter "Foo"
mydict = {answer: raw_input("Enter a value for {} ".format(answer)} Let's say I enter "5"
print mydict.get('Foo')
# 5
You can modify the globals dictionary if you wish.
globals()[raw_input()] = None # whatever "structure" you desire.
print foo
The above code would only work if the user actually inputted the string 'foo'. Otherwise it would say NameError: name 'foo' is not defined.
But yeah like Haidro said, whatever you're trying to do this for is probably a silly idea.
As #icktoofay noted in the comments, this will only work with the return value of the built-in function globals(). Modifying the return value of the built-in function locals() will not actually modify the local namespace.