Python One Line FizzBuzz

The FizzBuzz problem is a common exercise posed in code interviews to test your proficiency in writing simple Python code.

Problem: Print all numbers from 1-100 to the shell with three exceptions:

  • For each number divisible by three you print "Fizz",
  • For each number divisible by five you print "Buzz", and
  • For each number divisible by three and five you print "FizzBuzz".

Example: The first 15 numbers of the FizzBuzz sequence are the following.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

How to write a Python one-liner that solves this problem?

Here’s an interactive overview:

Exercise: Do both one-liners produce the same results? Run the code to check!

Let’s dive into those one-liners to gain a deeper understanding and improve your Python skills!

FizzBuzz One-Liner 1: Generator Expression + String Concatenation + Short Circuiting

The following one-liner solves the problem in an elegant way using a fine understanding of more advanced Python features (source).

print('\n'.join('Fizz' * (i%3==0) + 'Buzz' * (i%5==0) or str(i) for i in range(1,101)))

The one-liner creates a string using the join function with the newline character as a delimiter. Here’s a short explanation of the function:

The string.join(iterable) method concatenates all the string elements in the iterable (such as a list, string, or tuple) and returns the result as a new string. The string on which you call it is the delimiter stringβ€”and it separates the individual elements. For example, '-'.join(['hello', 'world']) returns the joined string 'hello-world'.

So, what’s the iterable, you pass into the join() function? It’s a generator expression of the form: expression for variable in context. You go over all integer values in the context 1 to 100 using the range() function. So, you obtain the remaining expression for i in range(1, 101). What’s the expression part?

It consists of three elements:

  • 'Fizz' * (i%3==0) — The modulo expression i%3==0 returns True only if the integer i is divisible by 3, otherwise it returns False. So, you multiply the string 'Fizz' either with True (=1) or with False (=0). As a result, you obtain the empty string '' in all cases except if the integer i is divisible by 3—in which case you obtain the string 'Fizz'.
  • 'Buzz' * (i%5==0)— The modulo expression i%5==0 returns True only if the integer i is divisible by 5, otherwise it returns False. So, you multiply the string 'Buzz' either with True (=1) or with False (=0). As a result, you obtain the empty string '' in all cases except if the integer i is divisible by 5—in which case you obtain the string 'Buzz'.
  • You use string concatenation to glue together the previously obtained strings. In most cases, this will be the empty string. If i is divisible by 3, you obtain the string 'Fizz'. If i is divisible by 5, you obtain the string 'Buzz'. And if i is divisible by 3 and 5, you obtain the string 'FizzBuzz'.
  • or str(i) — In the case you obtained a non-empty string in {'Fizz', 'Buzz', 'FizzBuzz'} in the previous step, the or operation simply returns this string. This is called short circuiting—and it’s used in many programming languages such as Python to improve efficiency of logical operations.
  • But if the string is empty, it is interpreted as a logical False. Thus, Python returns the second operand of the or operation. The second operand simply is the string representation of the integer i.

A very interesting implementation of the FizzBuzz problem indeed!

FizzBuzz One-Liner 2: Slicing

An alternative is given in the following nice one-liner (source):

for i in range(1, 101): print('FizzBuzz'[i*i%3*4:8--i**4%5] or i)

Wow—what a short and concise one-liner solution! But how does it work?

  • You iterate over all values from i=1 to i=100 and print a string. So far so good.
  • You use the or operation and slicing to determine the string 'FizzBuzz'[start:end] or i generates the output.
  • You use the property of short circuiting in Python: If 'FizzBuzz'[start:end] is empty, the integer i is returned, otherwise, the non-empty string is returned.
  • You carve out a substring from 'FizzBuzz' using slicing as follows.

Slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(lst), step=1). For example, the expression s[2:4] from string 'hello' carves out the slice 'll' and the expression s[:3:2] carves out the slice 'hl'.

In the example, you have the slicing operation 'FizzBuzz'[i*i%3*4:8--i**4%5].

  • start = i*i%3*4 — Note that the multiplication * and modulo operation % have the same priority, so they are evaluated from left to right. If integer i is divisible by 3, i*i is also divisible by 3, and the start index is 0. In all other cases, the start index is 4. Thus, the slice either starts with 'Fizz' or 'Buzz'.
  • stop = 8--i**4%5 — This is 4 in all cases except if the number i is divisible by 5, in which case this is 8.

So, there are four cases:

  • The number is divisible only by 3: start=0, stop=4 –> 'Fizz'
  • The number is divisible only by 5: start=4, stop=8 –> 'Buzz'
  • The number is divisible by both 3 and 5: start=0, stop=8 –> 'FizzBuzz'
  • The number is divisible by neither 3 nor 5: start = 4, stop=4 –> ''

Phew! This was a hard nut to crack, wasn’t it?

Python One-Liner 3: Map + Lambda

You can find detailed tutorials on the map and lambda functions here:

Those two functions can be used to solve the FizzBuzz problem (source):

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

It’s similar to Method 1 and by now you’re able to figure it out. Think of the different values the integer i can take.

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!