0

So this is more of a general question (as opposed to one looking for a specific line of code). Let's say you have a repeated process that generates an output. What is usually the best practice for storing the output so that it is easily accessible and ideally stored by name, where name is a variable as well? For example, I may have something like:

for i in range(5):
     output = i * 5
  • Is there a way to assign the output of each iteration of your loop to a variable? So that the variable name itself is produced by the loop. For example "trial0" is equal to 0, "trial1" is equal to 5, "trial2" is equal to 10, etc... And thus after doing the loop, I have constructed 5 new variables that I can use later on.
  • Would you be able to do something similar with dictionaries so that the key of the dictionary is produced by the loop and the items within that key are the output values?

In other words, what's the best way/best practice for storing the output of a repeated task and so that it is easily accessible and identifiable by something other than index value. I would like to easily refer to the output each time as opposed to referring it like list[1] or something.

  • Put them in a list or dictionary per e.g. https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables. It's unclear why you don't want to use an index but do want to jam the index into a variable name. – jonrsharpe Feb 23 '20 at 19:57
  • "Is there a way to assign the output of each iteration of your loop to a variable? So that the variable name itself is produced by the loop. " Generally speaking, no. And you shouldn't. Instead, you should use a *container*, like a `dict` or a `list`. If you have a dict, then yes, of course you can create keys/values in a loop. Have you tried anything? – juanpa.arrivillaga Feb 23 '20 at 20:00
  • How could `output` refer to the different objects? That doesn't make any sense. Why don't you want to use a `list` or a `dict`? – juanpa.arrivillaga Feb 23 '20 at 20:02
  • Well my loop will produce many vectors, objects, and calculations, one for each trial. It becomes a bit hard to keep track what's what if I have to do the following to refer to it such as output[0][3][2][7], aka the 8th item of the 3rd item of the 4th item of the 1st item. As opposed to just have a name for that particular thing, which ideally is generated via the loop. How do you name keys within a dictionary using a loop? – we_are_all_in_this_together Feb 23 '20 at 20:06
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Tomerikoo Feb 23 '20 at 20:07
  • 2
    Naming variables like `trial0`, `trial1`, `trial2` is a code smell indicating it wants to be a list, like `trial[0]`, `trial[1]`, `trial[2]`. – gilch Feb 23 '20 at 20:11
  • @Tomerikoo Yeah that seems to answer. Seems like best practice is just do the output[0][3][2][7] or just use a dictionary. How would I name the keys "trial1", "trial2", or is that not recommended either? – we_are_all_in_this_together Feb 23 '20 at 20:15
  • @gilch Yes sounds dumb but let's say you have trial1, which itself involves 2 trials, and for each of the two trials, I have calculated some metric such as standard deviation. Except for the two subtrials, only one of the standard deviations is important, the other I just want to keep for future reference. So I was hoping to give that particular standard deviation it's own variable name. That's a simple example but you get the point. – we_are_all_in_this_together Feb 23 '20 at 20:18
  • Also if I have lists of lists of lists of lists, tomorrow when I come back, I'll probably forget what the third item of the second item of the fifth item refers to so that's why I was hoping to name some of the important ones. – we_are_all_in_this_together Feb 23 '20 at 20:20
  • 2
    So use a class and give meaningful names to attributes – Tomerikoo Feb 23 '20 at 20:22
  • What Tomerikoo said (i.e. make a `Trial` class and have a list of `Trial`s.). Also see dataclasses, namedtuples, and SimpleNamespace. – gilch Feb 24 '20 at 06:07

2 Answers2

1

Not using a list makes no sense, since:

output = []
for i in range(5):
     output.append(i * 5)

print(output[0])
print(output[1])
...
print(output[4])

...is fine.

Brian Sidebotham
  • 1,706
  • 11
  • 15
1

yes, you can use a dict to store each iteration output:

output = {}

for i in range(5):
     output[i] = i * 5

and it is easily accessible, O(1) time complexity if you want to jump to a specific iteration output

kederrac
  • 16,819
  • 6
  • 32
  • 55