using generators with format strings

Terry Reedy tjreedy at udel.edu
Tue Jul 27 01:17:16 CEST 2004


"Calvin Spealman" <calvin at ironfroggy.com> wrote in message
news:1177931.tnjPut6V3j at ironfroggy.com...
> The generators are not list-type objects, but iterators. Because the %
> operator does not operate on iterators directly (because, presumably, you
> may be wanting to print the iterator itself, not the items it iterates
> over), you must construct a list out of it, which can be done very
easily,
> as you can see.
>
> x = "Hello, %s, this is a %s with %s and %s on top of %s" % [ i for i in
> myvalues()]
> y = "Yes it's true that %s has way too many %s's" % [i for i in
myvalues()]

list(myvalues()) is even more concise and more direct than the list comp

tjr






More information about the Python-list mailing list