Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to initialize Runtime array with name from the list

I have a list named
list_1 = ["File_A","File_B","File_C","File_D"]

I want to run a loop and get four arrays as

File_A = np.array(0)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

File_B = np.array(0)

File_C = np.array(0)

File_D = np.array(0)

Please reply if possible

>Solution :

I strongly discouraged you to use locals() (or globals() or vars()) to manipulate the variables dynamically:

list_1 = ['File_A', 'File_B', 'File_C', 'File_D']
for v in list_1:
    locals()[v] = np.array(0)

Output:

>>> File_A
array(0)

>>> File_B
array(0)

>>> File_C
array(0)

>>> File_D
array(0)

Note: File_A, File_B, … should be valid Python name identifiers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading