dimmg
dimmg

Reputation: 3835

Check if a string is equal with a variable name

I want to know if it is possible to compare if a string is equal with a variable name. For example I have the following declaration:

S=['A']
A=[['C'],['A','c','C']]
C=[['a'],['b'],['d','D']]
D=['A','e']
M=[S,A,C,D]
temp=[]

and

temp.append(S[0])
if S[0] in M :
...

Therefore I need to check if a string is equal with a variable name. Is it possible to do this? Thanks.

Upvotes: 0

Views: 3445

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124110

You'd have to derefence A first, using globals() for example:

if globals()[S[0]] in M:

However, you should rarely need to use this, however. Generally, you'd have such objects in a dictionary of your own, for example:

lists = {'A': [...], 'C': [...]}

and then you just test if S[0] in lists is True.

Upvotes: 1

Related Questions