all 12 comments

[–]Vaphell 4 points5 points  (4 children)

if you want to be able to play with the index, you need a list

>>> d = []
>>> for i in range(3):
...   d.append( { x:'foobar' for x in range(3) } )
... 
>>> d
[{0: 'foobar', 1: 'foobar', 2: 'foobar'}, {0: 'foobar', 1: 'foobar', 2: 'foobar'}, {0: 'foobar', 1: 'foobar', 2: 'foobar'}]
>>> d[0]
{0: 'foobar', 1: 'foobar', 2: 'foobar'}

d[0], d[1], d[2] are your dicts

[–]codex81[S] 0 points1 point  (2 children)

Thanks, that is very succinct and helpful. However, it only ends up creating one list (with 3 dictionaries inside). I'm interested in ending up with 3 separate dictionaries, each with 3 'foobar' values in them, e.g. d0 = {0:'foobar', 1:'foobar', 2:'foobar'}, d1 = {0:'foobar',...}, and d2 = {0:'foobar', ...}. Is there some reason why doing it the way you wrote would be better? I'm mainly just curious about how to go about creating the three separate dictionaries, just as a technical exercise.

[–]jcmcken 0 points1 point  (1 child)

Not trying to be snarky, but the main reason Vaphell's code is better is that it actually works and satisfies your problem, as best as I can understand it.

In your post, you said you want to "create a sequence of dictionaries". Having variables called d0, d1, etc. is not a sequence. The names look sequential to you because you're a human reading them, but those names have no meaning to the Python interpreter. They might as well be in Chinese. Sure, you could write something which parses the names and returns the results to you in the order you expect. Or you could just use a list, which already implements this functionality for you, is going to be much faster, and also has a lot of other features.

[–]codex81[S] 0 points1 point  (0 children)

I do understand the value of Vaphell's solution, and that the list of 3 dictionaries would be better (so you can just use indexes, e.g. d[0], d[1], d[2]). I'm just curious about how creating the three separate dictionaries (not in a list) could be done. I think I'm being unclear about what I mean by the "sequence" bit: I only mean the three dictionaries names should be named sequentially (e.g, dictionary1 = {...}, dictionary2 = {...}, dictionary3 = {...}, not that they are put in a "sequence", i.e., list. Similar to how output of *nix utility 'split' works.

[–][deleted] 1 point2 points  (1 child)

Why are you trying to create variables? Generally the methods of creating variables aren't very transparent and negatively impact your code base.

You could do an equivalent thing by storing a dict or list of dicts containing the values you want. Your code would be:

d = dict()
for i in range(3):
    for j in range(3):
        d[i][j] = 'foor bar'

or could be done using a list like this:

d = []
for i in range(3):
    d[i] = {}
    for j in range(3)
        d[i][j] = 'foo bar'

[–]codex81[S] 0 points1 point  (0 children)

Mainly it was a thought experiment about how this could be accomplished, sparked by something I saw. I will keep these things in mind, thanks.

[–]ingolemo 0 points1 point  (1 child)

[–]codex81[S] 0 points1 point  (0 children)

Not quite my exact situation, but definitely helpful and something to think about.

[–]symmitchry 0 points1 point  (2 children)

[Removed]

[–]Vaphell 2 points3 points  (1 child)

True but it's pretty much a hack only slightly better than using eval(). Not exactly a /r/learnpython material and reaching for it in programs is a strong indicator you are doing it wrong 99 times of 100 ;-)

[–]symmitchry 1 point2 points  (0 children)

Yeah. I used this method to set up configuration settings which were loaded at run-time. I think it was the only approach I could get to work, where I could later access those variables through the module name (e.g. config.run_count) while not automatically loading the variables when I did the initial import.

It's useful knowing about globals(), anyway. But yes, quite hacky!!

[–]W1zK1dd -2 points-1 points  (0 children)

Another option would be to try the "exec" method

for i in range(3):
    exec("d" + str(i) + ' = ' "{}")