[Tutor] newbie question

Trilok Khairnar trilokgk at gmail.com
Mon Oct 15 18:52:16 CEST 2007


I agree, was just trying to take home the point that if at all a format
string must be used (for whatever obscure reason), then it is not necesssary
to know beforehand the number of values in the dictionary (or the length of
list of its values) in order to to build the format string.
Hope that was parseably short, though not quite exactly pithy. :-)

Regards,
Trilok


-----Original Message-----
From: Luke Paireepinart [mailto:rabidpoobear at gmail.com]
Sent: Monday, October 15, 2007 10:03 PM
To: Trilok Khairnar
Cc: tutor at python.org
Subject: Re: [Tutor] newbie question

Trilok Khairnar wrote:
>> With this, and if you want to use your formatted print statements
>> instead
>>
> of the join, you could use something like
>
>> print "Specials: %s %s %s" % tuple(menu_specials.values()) which
>> turns the
>>
> output of menu_specials.values() (a list) into a tuple.
>
>> Disadvantage is that you'll need to know the number of values in
>> advance,
>>
> for the number of '%s' in your format string (alternatively,
>
>> you could build up the format string in a loop first). Note that
>> values()
>>
> bypasses the above list comprehensions entirely.
>
> With the following, you can do fine without knowing the length in advance.
>
> tpl = tuple(menu_specials.values())
> Print "Specials: " + ' '.join(["%s"*len(tpl)]) % tpl
>
I don't see the point of building a format string and then substituting for
it.
Why not just do:
print "Specials: " + ' '.join(menu_specials.values())

-Luke




More information about the Tutor mailing list