Creating Unique Dictionary Variables from List

Discussion in 'Python' started by Greg Corradini, Apr 11, 2007.

  1. Hello All,
    I'm attempting to create multiple dictionaries at once, each with unique
    variable names. The number of dictionaries i need to create depends on the
    length of a list, which was returned from a previous function.
    The pseudo code for this problem would be:

    returnedlist = [x,y,z]
    count = 0
    for i in returnedlist:
    if count < len(returnedlist):
    # then create a dictionary (beginning with variable dic) and add a
    unique ending such that
    # my final dictionary name would be dic + count for each i

    Any ideas about this?
    Greg
    --
    View this message in context: http://www.nabble.com/Creating-Unique-Dictionary-Variables-from-List-tf3560471.html#a9943321
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    Greg Corradini, Apr 11, 2007
    #1
    1. Advertising

  2. On Wed, 11 Apr 2007 09:57:35 -0700 (PDT), Greg Corradini
    <> declaimed the following in comp.lang.python:

    >
    > Hello All,
    > I'm attempting to create multiple dictionaries at once, each with unique
    > variable names. The number of dictionaries i need to create depends on the
    > length of a list, which was returned from a previous function.
    > The pseudo code for this problem would be:

    <snip>
    > Any ideas about this?


    Besides not posting three copies of the request? <G>

    The simplest scheme is to use a (master) dictionary.

    mDict = {}

    for c, d in enumerate(aList):
    mDict["dic_%s" % c] = {}
    #do something with the data in d?
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Apr 11, 2007
    #2
    1. Advertising

  3. On Wed, 11 Apr 2007 17:20:08 GMT, Dennis Lee Bieber
    <> declaimed the following in comp.lang.python:

    Talking to myself?

    >
    > The simplest scheme is to use a (master) dictionary.
    >
    > mDict = {}
    >
    > for c, d in enumerate(aList):
    > mDict["dic_%s" % c] = {}
    > #do something with the data in d?


    Unmentioned is that, if the name is just some constant with the
    ordinal position from the list... why bother? Which would you rather
    see/code:

    #using random to represent some means of arbitrary access
    for i in random.randint(0, len(mDict)-1):
    key = "This_is_the_dictionary_of_list_item_number_%s" % i
    something = mDict[key]["something"]

    or the simplicity of using a list to begin with:

    theDicts = []

    for c, d in enumerate(aList):
    theDicts.append({})
    #do something with the data in d, referencing theDicts[c]

    for i in random.randint(0, len(theDicts)-1):
    something=theDicts["something"]


    Now... If the "name" of the "variable" were based upon, say, the
    first subelement of each list entry... Then the mDict model makes sense:

    for d in aList:
    mDict[d[0]] = d[1:] #or whatever is needed to populate the sub
    dictionary
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Apr 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Corradini
    Replies:
    5
    Views:
    303
    Bruno Desthuilliers
    Apr 12, 2007
  2. Greg Corradini
    Replies:
    0
    Views:
    268
    Greg Corradini
    Apr 11, 2007
  3. james_027
    Replies:
    1
    Views:
    362
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. ToshiBoy
    Replies:
    6
    Views:
    885
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    391
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page