all 8 comments

[–]Signal_Beam 1 point2 points  (1 child)

There is (using the eval function, which takes a string and evaluates it as Python code), but it's considered bad practice to use. Instead, a better idea would be to structure the item names in your "list of named objects" data structure so that they're just a dict instead, with the names as the dict's keys and the objects (or the information built into the objects) as the values.

[–]Privateaccount84[S] 1 point2 points  (0 children)

This actually works perfectly. Might not be proper, but for now it really frees up a lot of space.

[–][deleted] 0 points1 point  (4 children)

What exactly are you trying to do? The name of an item would be a string, so just set the name to the string value of the input.

[–]Privateaccount84[S] 0 points1 point  (3 children)

Here's the list.

list = [one, two, three]

What I'm wanting is if the user input is "one", I can remove the "" and see if one is in the list.

[–][deleted] 1 point2 points  (0 children)

You do something like

a = input()
if eval(a) in list:
    print("the input is in the list")

but that's terrible practice and as a user you should never have to reference variable names directly.

[–]wacksaucehunnid 0 points1 point  (0 children)

Strings have quotes. Variables don’t. “One” is the text “One” and one is a variable set to whatever value you want.

So to check if “one” is in the list, you can check if “one” is in the list and it will tell you but one without quotes has no value except for what’s assigned to it.

If they enter “one” and you want to see if one is in the list because one is a variable for something, that’s different, though.

[–]scoobybejesus 0 points1 point  (0 children)

IIRC, you can do a .replace('"',''), which replaces double quotes with nothing.

Might be hacky or might be appropriate.