Feed on

to create a double dictionary in python, the following won’t work:

>>> x = dict()
>>> x[1][1] = “foo”

instead, you need only one set of brackets:

>>> x = dict()
>>> x = [1,1] = “foo”

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

9 Responses to “create a double dictionary in python”

  1. on 16 Oct 2008 at 6:50 pm Elmer

    That’s actually not a nested dictionary you’re making there, you’re just using a tuple for its key:

    >>> d = {}
    >>> d['key'] = ‘some value’
    >>> d['a','b'] = ‘nested?’
    >>> d
    {(‘a’, ‘b’): ‘nested?’, ‘key’: ‘some value’}

    If you want a dictionary inside a dictionary (which it seems is what you want), go about it like this:

    >>> d['nest'] = {}
    >>> d['nest']['key'] = ‘value’
    >>> d
    {(‘a’, ‘b’): ‘nested?’, ‘nest’: {‘key’: ‘value’}, ‘key’: ‘some value’}

    or, if you want to assign to a nested dictionary you’re not sure already exists:

    >>> d.setdefault(‘new key’, {})['level2'] = ‘auto_nest’
    >>> d
    {(‘a’, ‘b’): ‘nested?’, ‘new key’: {‘level2′: ‘auto_nest’}, ‘nest’: {‘key’: ‘value’}, ‘key’: ‘some value’}

  2. on 03 Sep 2010 at 5:02 am Henrik Karlsson

    Just because I searched like an idiot after adding as many dictionarys inside dictionarys as I wanted depending on how many objects was inside a List I’ll add that here since it’s fairly relevant. Hopefully someone will find it through google and will have some use of it =D

    This post was very helpful though in how I could achieve it.

    this is how it is done:

    for list in listOfPasses:

    completeList[i] = {listOfPasses[i] : {} }
    completeList[listOfPasses[i]] = {“key” : “value”}
    i = i + 1

    pretty simplified though, I also use list to set values and so on in passes, since this is done within maya to set attributes to renderman passes outside maya through a .conf file.

  3. on 05 Nov 2011 at 6:55 pm Shane O

    Here was an easy way that i done it.

    list1 = []

    dict1 = {}
    dict1["key1"] = “value1″


    dict2 = {}
    dict2["key2"] = “value2″


    # Retrieve the Dictionary’s #
    listdict1 = list1[0]
    listdict2 = list1[1]

    print “This is the first dict” , listdict1
    print “Here is the second dict” , listdict2

    P.S. I found this site trying to figure out how to do it. Haha

  4. on 13 Dec 2012 at 5:16 am WIlmer T

    Why not simply something like
    queue_params = ['queue','sndQueueSize','outQueueSize']
    countable_params = ['hangUps','estRetries']
    keys = queue_params + countable_params
    p_init = { ‘msg’:”", ‘count’: { ‘old’:0,’new’:0 } }
    params = dict.fromkeys(keys, p_init)

    Or am I barking up the wrong tree? ;)

  5. […] create a double dictionary in python » from the desk of stinkpot […]

  6. on 11 Jun 2020 at 6:06 am suba suba

    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to

  7. on 22 Jun 2020 at 7:24 am septic tank pumping

    Thanks again for the post.Really thank you! Much obliged.

  8. on 31 Aug 2020 at 9:56 am may ap trung mini

    writing like yours nowadays. I honestly appreciate people like you!

  9. on 18 Sep 2020 at 1:59 am Sammis Vapor

    I manage a vape store directory and we have had a posting from a vape shop in the USA that likewise advertises CBD product lines. A Calendar month afterwards, PayPal has contacted use to claim that our account has been limited and have requested us to get rid of PayPal as a payment solution from our vape shop web directory. We do not retail CBD products like CBD oil. We solely provide advertising and marketing solutions to CBD firms. I have looked into Holland & Barrett– the UK’s Major Health and wellness Store and if you take a good peek, you will witness that they offer a surprisingly extensive series of CBD products, particularly CBD oil and they also happen to accept PayPal as a payment solution. It appears that PayPal is applying twos sets of rules to different firms. Due to this limitation, I can no longer accept PayPal on my CBD-related web site. This has restricted my payment options and presently, I am greatly contingent on Cryptocurrency payments and direct bank transfers. I have checked with a solicitor from a Magic Circle law practice in The city of london and they explained that what PayPal is undertaking is completely illegal and discriminatory as it ought to be applying a consistent standard to all firms. I am still to get in touch with another attorney from a US law office in London to see what PayPal’s legal position is in the USA. For the time being, I would be extremely appreciative if anyone here at targetdomain could provide me with substitute payment processors/merchants that work with CBD companies.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.