1

I don't know the best way to describe what I am really looking for probably this example is the best for explaining my problem:

I have the following list of strings (my object names):

mynames = ["A","B","C"]

my objects itself are defined as:

A = "Hello"
B = 12
C = "This is C"

Now I'd like to create a comma-separated string with all values of the objects mentioned in "mynames" like:

myresult = "Hallo,12,This is C"

of course I could do it manually like:

",".join([A,str(B),C])

but is there a way to create the result based on the list "mynames", something like ",".join(mynames)??

thanks,

/j

2
  • How about mynames list storing the objects instead of strings?, is there any reason for it? Commented Oct 11, 2012 at 8:41
  • Don't do that. Instead, make mynames a dictionary. Commented Oct 11, 2012 at 8:56

3 Answers 3

4

The obvious answer is to use globals() or locals() as in

lst = [globals()[x] for x in 'A', 'B', 'C']

and the correct one is

 don't do that

Every time you attempt to manipulate variable names in python you're doing a wrong thing. Use an appropriate data structure, like dict.

Sign up to request clarification or add additional context in comments.

3 Comments

SO should change the syntax highlighting for some important messages, such as don't do that
I believe there is a way to indicate the language for syntax highlighting -- some sort of funky comment -- but I can't remember it.
2

as mentioned in other answers, you can use eval, literal-eval, globals, locals, or other such nonsense to complete your task - but your approach is wrong.

as i think is mentioned in other answers, you are better off using a different data structure, a dictionary perhaps. for instance, in your example:

A = "Hello"
B = 12
C = "This is C"

can be done with:

my_dict = {}
my_dict["A"] = "Hello"
my_dict["B"] = 12
my_dict["C"] = "This is C"

and then you can simply do this:

mynames = ["A","B","C"]
print [my_dict[key] for key in mynames]

on the console, this is the result:

>>> my_dict = {}
>>> my_dict["A"] = "Hello"
>>> my_dict["B"] = 12
>>> my_dict["C"] = "This is C"
>>> mynames = ["A","B","C"]
>>> print [my_dict[key] for key in mynames]
['Hello', 12, 'This is C']

making them into one string is simple now, and im sure you can take it from here.

2 Comments

Thanks, works perfectly of course. I have to focus more on the usage of dictionaries... :)
dictionaries are great. and there are many more types of data structures, each with their own ideal uses. dicts however, are a great backbone. when dealing with many different variables that are all related - a dict is almost a must. and since python doesnt care much for types, your dictionary can of course, contain anything, even more dictionaries.
0

eval is your friend here:

",".join([ str(eval(e)) for e in mynames ])

Comments

Your Answer

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.