Improve your programming skills

How to create python variables in a loop from JSON, dict, list

https://stackoverflow.com/questions/11319909/creating-new-variables-in-loop-with-names-from-list-in-python

I think dictionaries are more suitable for this purpose:

>>> name = ['mike', 'john', 'steve']   

>>> age = [20, 32, 19] 

>>> dic=dict(zip(name, age))

>>> dic['mike']
20
>>> dic['john']
32

fly you can useĀ globals()[]:

>>> for x,y in zip(name, age):
    globals()[x] = y


>>> mike
20
>>> steve
19
>>> john
32

Leave a comment

Your email address will not be published. Required fields are marked *