Building a dictionary from a tuple of variable length

Discussion in 'Python' started by bg_ie@yahoo.com, Mar 5, 2007.

  1. Guest

    Hi,

    I have the following tuple -

    t = ("one","two")

    And I can build a dictionary from it as follows -

    d = dict(zip(t,(False,False)))

    But what if my tuple was -

    t = ("one","two","three")

    then I'd have to use -

    d = dict(zip(t,(False,False,False)))

    Therefore, how do I build the tuple of Falses to reflect the length of
    my t tuple?

    Thanks for your help,

    Barry.
     
    , Mar 5, 2007
    #1
    1. Advertising

  2. In <>, bg_ie wrote:

    > Therefore, how do I build the tuple of Falses to reflect the length of
    > my t tuple?


    In [1]: dict.fromkeys(('one', 'two', 'three'), False)
    Out[1]: {'three': False, 'two': False, 'one': False}

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Mar 5, 2007
    #2
    1. Advertising

  3. Peter Otten Guest

    wrote:

    > I have the following tuple -
    >
    > t = ("one","two")
    >
    > And I can build a dictionary from it as follows -
    >
    > d = dict(zip(t,(False,False)))
    >
    > But what if my tuple was -
    >
    > t = ("one","two","three")
    >
    > then I'd have to use -
    >
    > d = dict(zip(t,(False,False,False)))
    >
    > Therefore, how do I build the tuple of Falses to reflect the length of
    > my t tuple?


    For dictionaries there is a special method:

    >>> dict.fromkeys(("one", "two", "three"), False)

    {'three': False, 'two': False, 'one': False}

    When you are just interested in the list of tuples, use repeat():

    >>> from itertools import repeat
    >>> zip("abc", repeat(False))

    [('a', False), ('b', False), ('c', False)]

    Peter
     
    Peter Otten, Mar 5, 2007
    #3
  4. kirjoitti:
    > Hi,
    >
    > I have the following tuple -
    >
    > t = ("one","two")
    >
    > And I can build a dictionary from it as follows -
    >
    > d = dict(zip(t,(False,False)))
    >
    > But what if my tuple was -
    >
    > t = ("one","two","three")
    >
    > then I'd have to use -
    >
    > d = dict(zip(t,(False,False,False)))
    >
    > Therefore, how do I build the tuple of Falses to reflect the length of
    > my t tuple?
    >
    > Thanks for your help,
    >
    > Barry.
    >


    Another variation:

    d = dict((x, False) for x in t)

    Cheers,
    Jussi
     
    Jussi Salmela, Mar 5, 2007
    #4
  5. Hi,

    > Therefore, how do I build the tuple of Falses to reflect the length of my t tuple?


    Yet another solution :

    d = dict(zip(t,[False]*len(t)))

    Pierre
     
    Pierre Quentel, Mar 5, 2007
    #5
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    841
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    1,052
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    641
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    874
    Antoon Pardon
    Mar 30, 2005
  5. Helmut Jarausch

    variable length tuple assignment

    Helmut Jarausch, Feb 25, 2009, in forum: Python
    Replies:
    3
    Views:
    583
    Steven D'Aprano
    Feb 25, 2009
Loading...

Share This Page