Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Building a dictionary from a tuple of variable length

Reply
Thread Tools

Building a dictionary from a tuple of variable length

 
 
bg_ie@yahoo.com
Guest
Posts: n/a
 
      03-05-2007
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.

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      03-05-2007
In <. com>, 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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
Jussi Salmela
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
Pierre Quentel
Guest
Posts: n/a
 
      03-05-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
variable length tuple assignment Helmut Jarausch Python 3 02-25-2009 11:08 PM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57