0

So I have something like this:

for j, key in zip(range(10), SomeNestedDictionary.keys()):
      dictionary1 = SomeNestedDictionary[key]

However here dictionary1 , is always the same name. Instead I would like a way to have dictionary_someNameDependentOnTheIndexOfTheLoop, so that I will create 10 dictionaries with different names. How do I do this? How do I control the name of the dictionary?

Also, is there a way of doing something similar,but with a list? I.e. changing the name of the list to be created at each iteration of a loop?

Notice this questions is different from the one marked as duplicate. In that questions simple lists could have solved the problem, here they cannot.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Euler_Salter
  • 3,271
  • 8
  • 33
  • 74
  • I don't think that question has been answered satisfactorily – Euler_Salter Jul 05 '17 at 09:19
  • This is a different question – Euler_Salter Jul 05 '17 at 09:28
  • 1
    I disagree. You want to dynamically create names in your global scope. This is covered in the duplicate with exec (although generally *not* suggested; a `dict` is a way better approach, see the second answer). I think pinging will work here, @Jean-François Fabre, so he can also explain how this is a good duplicate target. – Dimitris Fasarakis Hilliard Jul 05 '17 at 10:39
  • @Euler_Salter: I don't see how it doesn't answer:: in the link there are a lot of answers explaining how to create variables on the fly, or store in dictionaries. In your case, I would do a dictionary of dictionaries, not generated variables (this idea of generating variable names generally sucks when you want to use them) – Jean-François Fabre Jul 05 '17 at 10:51

0 Answers0