advanced listcomprehenions?

cirfu circularfunc at
Wed Jun 18 23:42:00 CEST 2008

I am wondering if it is possible to write advanced listcomprehensions.

For example:
"""Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz"."""
Obv it doesnt have to be a list according tot hat definition but
suppose i want to generate that list.

>>> [["Fizzbuzz",x] for x in xrange(1,101) if x%3 == 0 and x%5 == 0]
[['Fizzbuzz', 15], ['Fizzbuzz', 30], ['Fizzbuzz', 45], ['Fizzbuzz',
60], ['Fizzbuzz', 75], ['Fizzbuzz', 90]]

is not what i want. the following does the trick but is ldo not a

for i in xrange(1,101):
    s = ""
    if i%3 == 0:
        s += "Fizz"
    if i%5 == 0:
        s += "Buzz"
    if s:
        print "%d : %s" % (i,s)
        print i

or to generate a lisrt but not by listcomprehsnion:
map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and
or (not x%5 and "Buzz") or x, xrange(1,101))

More information about the Python-list mailing list