Linked Questions
181 questions linked to/from How can you dynamically create variables?
566 votes
19 answers
282k views
How do I create variable variables?
I know that some other languages, such as PHP, support a concept of "variable variable names" - that is, the contents of a string can be used as part of a variable name. I heard that this is ...
10 votes
2 answers
76k views
Creating dynamically named variables from user input [duplicate]
I'm just learning to program and am learning Python as my first language. As an exercise I'm trying to write an address book program. New contact are created by the user using the command prompt. ...
0 votes
4 answers
4k views
In Python, how to create a large number of variables in a loop? [duplicate]
I'm looking for a way, in python 3.2, to create a large number of variables and assign them values. Something like X = 10 Y = 10 A = 0 B = 0 while X >= 0: while Y >= 0: cell[C]X[A] =...
2 votes
5 answers
13k views
(Python) Creating a list with a unique automatically generated name [duplicate]
I am trying to automate populating a town by randomly generating households. I generate the name of the town, generate the number of households, the last name of each household and number of occupants ...
0 votes
2 answers
18k views
Python create variable according to for loop index [duplicate]
I am trying to create variable for each iteration of the for loop: for i in range(10): x(i) = 'abc' so that I would get x1, x2, x3, x4, .... x10, all equal to 'abc' Anyone know how to do it? Thanks!...
1 vote
6 answers
9k views
Dynamic variable creation [duplicate]
My question is how to create variables "on the fly". I'm trying to generate an object with a random set of attributes. from random import randint, choice class Person(object): def ...
3 votes
5 answers
6k views
Run a loop to generate variable names in Python [duplicate]
I'm trying to import datasets which have the following filenames (phone1, phone2, etc) df1 = pd.read_csv(r'C:\Users\...\phone1.csv') df2 = pd.read_csv(r'C:\Users\...\phone2.csv') df3 = pd.read_csv(r'C:...
1 vote
2 answers
19k views
PYTHON - creating multiple lists in a loop [duplicate]
I'm trying to put together a programming project at my university, but I'm stuck. The "math" part I've got covered, but I need help with lists and loops in Python. I'm working with graphs, let me ...
0 votes
1 answer
13k views
Pandas: Dictionary of Dataframes [duplicate]
I have a function that I made to analyze experimental data (all individual .txt files) This function outputs a dictionary ({}) of Pandas Dataframes Is there a efficient way to iterate over this ...
2 votes
2 answers
6k views
String Variables to Variable Name? [duplicate]
I was just wondering if there was a way I could create variables from string like in the code below, I can create a file which includes the variable subject in the filename. filehandle = open('...
-1 votes
2 answers
8k views
defining variable names with integer values defined by user in python [duplicate]
I am currently trying to write a simple program to calculate probabilities of possible combinations with dice of different sizes (e.g. : 6, 12, 4, 20). The problem i have is that it is supposed to be ...
0 votes
3 answers
7k views
dynamically creating variables in python [duplicate]
I have a file that stores variable names and values like below: name,David age,16 score,91.2 ... I want to write a python script that reads the file and automatically creates variables in an object ...
3 votes
2 answers
3k views
Instantiate a class using a variable for its name [duplicate]
I'm a little new to Python, and was hoping that someone might be able to help me with a problem I've got. I have the need to instantiate multiple objects using the variables in a list. Let's say I ...
0 votes
5 answers
2k views
Python name variable from string [duplicate]
Is it possible to create a variable name based on the value of a string? I have a script that will read a file for blocks of information and store them in a dictionary. Each block's dictionary will ...
0 votes
1 answer
3k views
Invalid syntax using np.zeros [duplicate]
I would like to write this: x = 'something_{}'.format(1) exec('{} = {}'.format(x,np.zeros((2,2)))) Problem: I get SyntaxError: invalid syntax and I don't know how to solve it. Someone has an idea?