1

I ask the user of my program to input the number of datasets he/she wants to investigate, e.g. three datasets. Accordingly, I should then create three dictionaries (dataset_1, dataset_2, and dataset_3) to hold the values for the various parameters. Since I do not know beforehand the number of datasets the user wants to investigate, I have to create and name the dictionaries within the program.

Apparently, Python does not let me do that. I could not rename the dictionary once it has been created.

I have tried using os.rename("oldname", "newname"), but that only works if I have a file stored on my computer hard disk. I could not get it to work with an object that lives only within my program.

number_sets = input('Input the number of datasets to investigate:')

for dataset in range(number_sets):
    init_dict = {}
    # create dictionary name for the particular dataset
    dict_name = ''.join(['dataset_', str(dataset+1)])
    # change the dictionary´s name
    # HOW CAN I CHANGE THE DICTIONARY´S NAME FROM "INIT_DICT"
    # TO "DATASET_1", WHICH IS THE STRING RESULT FOR DICT_NAME?

I would like to have in the end

dataset_1 = {} dataset_2 = {}

and so on.

Carvalho
  • 11
  • 1
  • 3
  • 2
    Rule of thumb: if you're trying to create a bunch of variables with names that are all identical except they end with different numbers, you should be using a list instead. `datasets = []` outside the loop, and `datasets.append(init_dict)` inside the loop. Then you can get the third dataset with `datasets[2]`. – Kevin Mar 25 '19 at 15:15
  • You may need to copy them and assign them a new name. – Eddwin Paz Mar 25 '19 at 15:16

4 Answers4

2

You don't (need to). Keep a list of data sets.

datasets = []
for i in range(number_sets):
    init_dict = {}
    ...
    datasets.append(init_dict)

Then you have datasets[0], datasets[1], etc., rather than dataset_1, dataset_2, etc.

Inside the loop, init_dict is set to a brand new empty directory at the top of each iteration, without affecting the dicts added to datasets on previous iterations.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

If you want to create variables like that you could use the globals

number_sets = 2
for dataset in range(number_sets):
    dict_name = ''.join(['dataset_', str(dataset+1)])
    globals() [dict_name] = {}

print(dataset_1)
print(dataset_2)

However this is not a good practice, and it should be avoided, if you need to keep several variables that are similar the best thing to do is to create a list.

Mntfr
  • 483
  • 6
  • 20
0

You can use a single dict and then add all the data sets into it as a dictionary:

all_datasets = {}

for i in range(number_sets):
    all_datasets['dataset'+str(i+1)] = {}

And then you can access the data by using:

all_datasets['dataset_1']
heena bawa
  • 818
  • 6
  • 5
0

This question gets asked many times in many different variants (this is one of the more prominent ones, for example). The answer is always the same:

It is not easily possible and most of the time not a good idea to create python variable names from strings.

The more easy, approachable, safe and usable way is to just use another dictionary. One of the cool things about dictionaries: any object can become a key / value. So the possibilities are nearly endless. In your code, this can be done easily with a dict comprehension:

number_sets = int(input('Input the number of datasets to investigate:'))  # also notice that you have to add int() here
data = {''.join(['dataset_', str(dataset + 1)]): {} for dataset in range(number_sets)}
print(data)

>>> 5
{'dataset_1': {}, 'dataset_2': {}, 'dataset_3': {}, 'dataset_4': {}, 'dataset_5': {}}     

Afterwards, these dictionaries can be easily accessed via data[name_of_dataset]. Thats how it should be done.

Flob
  • 898
  • 1
  • 5
  • 14
  • Thanks everybody for answering and commenting on my questions. You have pointed out to me the way to go. I will go through all the answers carefully to figure out the best solution to my problem. – Carvalho Mar 25 '19 at 17:41