Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating variable names on fly in python [duplicate]

Tags:

python

Is there a way I can generate variable names in python in a loop and assign values to them? For example, if I have

prices = [5, 12, 45] 

I want

price1 = 5 price2 = 12 price3 = 45 

Can I do this in a loop or something instead of manually assigning price1 = prices[0], price2 = prices[1] etc.

Thank you.

EDIT

Many people suggested that I write a reason for requiring this. First, there have been times where I have thought this may be more convenient than using a list...I don't remember exactly when, but I think I have thought of using this when there are many levels of nesting. For example, if one has a list of lists of lists, defining variables in the above way may help reduce the level of nesting. Second, today I thought of this when trying to learn use of Pytables. I just came across Pytables and I saw that when defining the structure of a table, the column names and types are described in the following manner:

class TableFormat(tables.IsDescription):     firstColumnName = StringCol(16)     secondColumnName = StringCol(16)     thirdColumnName = StringCol(16) 

If I have 100 columns, typing the name of each column explicitly seems a lot of work. So, I wondered whether there is a way to generate these column names on the fly.

like image 202
Curious2learn Avatar asked Oct 24 '10 22:10

Curious2learn


People also ask

How do you dynamically create variable names in Python?

Use a Dictionary to Create a Dynamic Variable Name in Python It is written with curly brackets {} . In addition to this, dictionaries cannot have any duplicates. A dictionary has both a key and value, so it is easy to create a dynamic variable name using dictionaries.

Can you reuse variable names in Python?

In Python, we may reuse the same variable to store values of any type. A variable is similar to the memory functionality found in most calculators, in that it holds one value which can be retrieved many times, and that storing a new value erases the old.

Can two variables have the same name Python?

Variable names in Python are case sensitive. Two variables with the same name but different casing are considered as two different variables. For example, websitename is considered a different variable to websiteName because of the capitalization of the letter “N“.


2 Answers

If you really want to create them on the fly you can assign to the dict that is returned by either globals() or locals() depending on what namespace you want to create them in:

globals()['somevar'] = 'someval' print somevar  # prints 'someval' 

But I wouldn't recommend doing that. In general, avoid global variables. Using locals() often just obscures what you are really doing. Instead, create your own dict and assign to it.

mydict = {} mydict['somevar'] = 'someval' print mydict['somevar'] 

Learn the python zen; run this and grok it well:

>>> import this 
like image 112
kanaka Avatar answered Oct 12 '22 12:10

kanaka


Though I don't see much point, here it is:

for i in xrange(0, len(prices)):     exec("price%d = %s" % (i + 1, repr(prices[i]))); 
like image 41
Tim Čas Avatar answered Oct 12 '22 12:10

Tim Čas