create JSON with multiple dictionaries, Python
Question: I have this code:

    
    
    >>> import simplejson as json
    >>> keys = dict([(x, x**3) for x in xrange(1, 3)])
    >>> nums = json.dumps(keys, indent=4)
    >>> print nums
    {
        "1": 1,
        "2": 8
    }
    

But I want to create a loop to make my output looks like this:

    
    
    [
        {
            "1": 1,
            "2": 8
        },
        {
            "1": 1,
            "2": 8
        },
        {
            "1": 1,
            "2": 8
        }
    ]
    


Answer: Your desired output is not valid JSON. I think what you probably meant to do
was to append multiple dictionaries to a list, like this:

    
    
    >>> import json
    >>> multikeys = []
    >>> for i in range(3):
    ...    multikeys.append(dict([(x, x**3) for x in xrange(1, 3)]))
    ... 
    >>> print json.dumps(multikeys, indent=4)
    [
        {
            "1": 1, 
            "2": 8
        }, 
        {
            "1": 1, 
            "2": 8
        }, 
        {
            "1": 1, 
            "2": 8
        }
    ]
    

