Python variable as a string

J

Jake Angulo

Sorry this is a very basic question.

I have a list *var* which after some evaluation I need to refer to *var* as
a string.

Pseudocode:

var = ['a', 'b' , 'c' , 'd']
adict = dict(var='string', anothervar='anotherstring')
anotherdict = dict()
if <condition>:
anotherdict[akey] = adict['var']


Basically im evaluating the list *var*, and if true, i want to use *var* as
a string so that i can refer to a key-value pair in *adict *(whose key name
is also var for convenience).
*
*
Or maybe i should do things differently?

Any help and code will be appreciated!
 
N

Neil Cerutti

I have a list *var* which after some evaluation I need to refer
to *var* as a string.

You must make a str version of var.
Pseudocode:

var = ['a', 'b' , 'c' , 'd']
adict = dict(var='string', anothervar='anotherstring')
anotherdict = dict()
if <condition>:
anotherdict[akey] = adict['var']

anotherdict[akey] = adict[str(var)]

Will actually work, though you might prefer:

anotherdict[akey] = adict[''.join(var)]

Try them out and see.
 
S

Steven D'Aprano

Sorry this is a very basic question.

Not so much "basic" as confusing.
I have a list *var* which after some evaluation I need to refer to *var*
as a string.

Pseudocode:

var = ['a', 'b' , 'c' , 'd']
adict = dict(var='string', anothervar='anotherstring')

This creates a dict with two keys, "var" and "anothervar". If you print
it, you will get this:

{'var': 'string', 'anothervar': 'anotherstring'}

Is that what you intended? If not, what did you intend?

anotherdict = dict()
if <condition>:
anotherdict[akey] = adict['var']

I don't understand what this code has to do with your question. Your
explanation below doesn't seem to have anything to do with the code you
show here. You don't evaluate the list var, or test it in a truth
context. Apart from wrapping condition in angle brackets, for no reason I
understand, the above is perfectly fine Python code (except, of course,
condition and akey are undefined).

Basically im evaluating the list *var*, and if true, i want to use *var*
as a string so that i can refer to a key-value pair in *adict *(whose
key name is also var for convenience).

I don't understand what you are trying to accomplish, but I have two
guesses. If you want a string "var", just type "var" in quotation marks,
like you do above.

If you want to use the *contents* of variable var as a string, just call
the str() function on it:

py> alist = ['a', 'b' , 'c' , 'd']
py> key = str(alist)
py> adict={}
py> adict[key] = "whatever you like"
py> adict
{"['a', 'b', 'c', 'd']": 'whatever you like'}


Or if you prefer:

py> {key: "whatever"}
{"['a', 'b', 'c', 'd']": 'whatever'}


If you want something else, you'll need to explain more carefully what
you want.

Or maybe i should do things differently?

Possibly. What sort of things did you have in mind? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top