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) for each i
    with a unique name such that
    # my unique name would be dic + count

    Any ideas about this?
    Greg


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

  2. Greg Corradini a écrit :
    > 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) for each i
    > with a unique name such that
    > # my unique name would be dic + count
    >
    > Any ideas about this?


    Yes : use a dict to store your dicts:

    returnedlist = [x,y,z]
    dicts = dict()
    for num, item in enumerate(returnedlist):
    dicts['dict%s' % num] = dict()
    Bruno Desthuilliers, Apr 11, 2007
    #2
    1. Advertising

  3. Bruno,
    Your help is much appreciated. I will give this a try tomorrow morning and
    get back on how it works.


    Bruno Desthuilliers wrote:
    >
    > Greg Corradini a écrit :
    >> 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) for each
    >> i
    >> with a unique name such that
    >> # my unique name would be dic + count
    >>
    >> Any ideas about this?

    >
    > Yes : use a dict to store your dicts:
    >
    > returnedlist = [x,y,z]
    > dicts = dict()
    > for num, item in enumerate(returnedlist):
    > dicts['dict%s' % num] = dict()
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >


    --
    View this message in context: http://www.nabble.com/Creating-Unique-Dictionary-Variables-from-List-tf3560643.html#a9947284
    Sent from the Python - python-list mailing list archive at Nabble.com.
    Greg Corradini, Apr 11, 2007
    #3
  4. Greg Corradini a écrit :
    > Bruno,
    > Your help is much appreciated.


    Then give thanks to Dennis too !-)

    > I will give this a try tomorrow morning and
    > get back on how it works.


    Don't worry, it just works - and it's the idiomatic solution to the
    problem you described.
    Bruno Desthuilliers, Apr 11, 2007
    #4
  5. On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:

    > Greg Corradini a écrit :
    >> 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) for each i
    >> with a unique name such that
    >> # my unique name would be dic + count
    >>
    >> Any ideas about this?

    >
    > Yes : use a dict to store your dicts:
    >
    > returnedlist = [x,y,z]
    > dicts = dict()
    > for num, item in enumerate(returnedlist):
    > dicts['dict%s' % num] = dict()


    Given that num is unique each time around the loop, what do you gain by
    using 'dictN' for the key instead of just N (=num)?

    returnedlist = [x,y,z]
    dicts = {}
    for num, item in enumerate(returnedlist):
    # presumably you would use item somewhere
    dicts[num] = {item: None}

    And that suggests that storing the dicts in a dict may be unnecessary --
    just put them in a list:

    returnedlist = [x,y,z]
    dicts = [None] * len(returnedlist)
    for num, item in enumerate(returnedlist):
    dicts[num] = {item: None}



    --
    Steven.
    Steven D'Aprano, Apr 12, 2007
    #5
  6. Steven D'Aprano a écrit :
    > On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:
    >
    >
    >>Greg Corradini a écrit :
    >>
    >>>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) for each i
    >>>with a unique name such that
    >>> # my unique name would be dic + count
    >>>
    >>>Any ideas about this?

    >>
    >>Yes : use a dict to store your dicts:
    >>
    >>returnedlist = [x,y,z]
    >>dicts = dict()
    >>for num, item in enumerate(returnedlist):
    >> dicts['dict%s' % num] = dict()

    >
    >
    > Given that num is unique each time around the loop, what do you gain by
    > using 'dictN' for the key instead of just N (=num)?


    The OP wanted such names, that's all.
    Bruno Desthuilliers, Apr 12, 2007
    #6
    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:
    0
    Views:
    245
    Greg Corradini
    Apr 11, 2007
  2. Greg Corradini
    Replies:
    2
    Views:
    593
    Dennis Lee Bieber
    Apr 11, 2007
  3. james_027
    Replies:
    1
    Views:
    311
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. ToshiBoy
    Replies:
    6
    Views:
    832
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    341
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page