Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FizzBuzz one-liner in Python 3
# Using a lambda function
print(list(map(lambda i: "Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i), range(1,101))))
# Using a for loop
for i in range(1, 101): print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i))
@JonathanDagan
Copy link

JonathanDagan commented Sep 3, 2020

list comprehension verion:

[print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or i) for i in range(101)]

also the:

str(i)

isn't really necessary

otherwise nice implementation

@leoschur
Copy link

leoschur commented Mar 23, 2021

Neat!
Didn´t like the print in the loop though:

FizzBuzzChallenge = "\n".join(["Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) for i in range(101)])
print(FizzBuzzChallenge)

In this case str(i) is necessary.

@khanfarhan10
Copy link

khanfarhan10 commented Oct 3, 2021

print('\n'.join(list(map(lambda i: "Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i), range(1,int(input())+1)))))

@Dummyapt
Copy link

Dummyapt commented Oct 17, 2021

print(list(map(lambda i: "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or str(i), range(0, 101))))

@miron
Copy link

miron commented May 20, 2022

@JonathanDagan really nice! But this one actually works:

_=[print("Fizz"*(i%3==0)+"Buzz"*(i%5==0)or i)for i in range(1,101)]

You still get the output, but the print doesn't return the list with None values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment