Click here to Skip to main content
15,902,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the following code (this sudo code that reflect the real values in my code)

i have alist :

Python
virt_who_attached_to_std_vdc = ['A', 'B' , 'c' ]


i want to check if an item in that list ,but i want check the list name as the below variable:

Python
vdc_sub = 'std'
	sub = 'virt_who_attached_to_'+vdc_sub+'_vdc'

if i am printing the value of "sub" i am getting the same name as the original list ,but this is a string of course:

print(sub)
virt_who_attached_to_std_vdc

so if i will check if 'a' is in sub like this :

Python
if 'A' in sub:


its doesnt print anything ,how do i represent the sub variable as the original virt_who_attached_to_std_vdc list?

What I have tried:

tried to convert sub it to list
Python
sublist = list(sub)
sublist
['v', 'i', 'r', 't', '_', 'w', 'h', 'o', '_', 'a', 't', 't', 'a', 'c', 'h', 'e', 'd', '_', 't', 'o', '_', 's', 't', 'd', '_', 'v', 'd', 'c']
Posted
Updated 26-Dec-17 6:06am
v2

1 solution

If I understand correctly, you want to get the list that has the name stored in sub and check if 'A' is in that list.

First you need to know: is the list a global variable, or a local variable?

If it is a global variable, you can do this:
Python
if 'A' in globals()[sub]:
    # code
globals() returns all global variables as a dict, so you can get your list with the [sub] syntax.

If the list is a local variable, you have to use locals() instead of globals():
Python
if 'A' in locals()[sub]:
    # code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900