This article answers a number of questions how to accomplish different things with a Python array in one line. By studying these questions, you’ll become a better coder. So, let’s roll up your sleeves and get started!
Python One Line Print Array
If you just want to know the best way to print an array (list) in Python, here’s the short answer:
Pass a list as an input to the print() function in Python.
Use the asterisk operator * in front of the list to “unpack” the list into the print function.
Use the sep argument to define how to separate two list elements visually.
Here’s the code:
# Create the Python List
lst = [1, 2, 3, 4, 5] # Use three underscores as separator
print(*lst, sep='___')
# 1___2___3___4___5 # Use an arrow as separator
print(*lst, sep='-->')
# 1-->2-->3-->4-->5
Try It Yourself in Our Interactive Code Shell:
This is the best and most Pythonic way to print a Python array list. If you still want to learn about alternatives—and improve your Python skills in the process of doing so—read the following tutorial!
The most basic ternary operatorx if c else y returns expression x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative expression y.
Here’s a minimal example:
var = 21 if 3<2 else 42
# var == 42
While you read through the article to boost your one-liner power, you can listen to my detailed video explanation:
Method 1: If the loop body consists of one statement, simply write this statement into the same line: for i in range(10): print(i). This prints the first 10 numbers to the shell (from 0 to 9).
Method 2: If the purpose of the loop is to create a list, use list comprehension instead: squares = [i**2 for i in range(10)]. The code squares the first ten numbers and stores them in the array list squares.
Let’s have a look at both variants in more detail in the following article:
How to iterate over an array in a single line of code?
Say, you’ve given an array (list) lst and you want to iterate over all values and do something with them. You can accomplish this using list comprehension:
lst = [1, 2, 3]
squares = [i**2 for i in lst]
print(squares)
# [1, 4, 9]
You iterate over all values in the array lst and calculate their square numbers. The result is stored in a new array list squares.
You can even print all the squared array values in a single line by creating a dummy array of None values using the print() function in the expression part of the list comprehension statement:
This creates an array of ten elements filled with the value 0. You can also fill the array with other elements by replacing the 0 with the desired element—for example, [None] * 10 creates a list of ten None elements.
Python Initialize Array One Line
There are many ways of creating an array (list) in Python. Let’s get a quick overview in the following table:
Code
Description
[]
Square bracket: Initializes an empty list with zero elements. You can add elements later.
[x1, x2, x3, … ]
List display: Initializes an empty list with elements x1, x2, x3, … For example, [1, 2, 3] creates a list with three integers 1, 2, and 3.
[expr1, expr2, ... ]
List display with expressions: Initializes a list with the result of the expressions expr1, expr2, … For example, [1+1, 2-1] creates the list [2, 1].
[expr for var in iter]
List comprehension: applies the expression expr to each element in an iterable.
list(iterable)
List constructor that takes an iterable as input and returns a new list.
[x1, x2, ...] * n
List multiplication creates a list of n concatenations of the list object. For example [1, 2] * 2 == [1, 2, 1, 2].
You can play with some examples in our interactive Python shell:
How can you filter an array in Python using an arbitrary condition?
The most Pythonic way of filtering an array is the list comprehension statement [x for x in list if condition]. You can replace condition with any function of x you would like to use as a filtering criterion.
For example, if you want to filter all elements that are smaller than, say, 10, you’d use the list comprehension statement [x for x in list if x<10] to create a new list with all list elements that are smaller than 10.
Here are three examples of filtering a list:
Get elements smaller than eight: [x for x in lst if x<8].
Get even elements: [x for x in lst if x%2==0].
Get odd elements: [x for x in lst if x%2].
lst = [8, 2, 6, 4, 3, 1] # Filter all elements <8
small = [x for x in lst if x<8]
print(small) # Filter all even elements
even = [x for x in lst if x%2==0]
print(even) # Filter all odd elements
odd = [x for x in lst if x%2]
print(odd)
The output is:
# Elements <8
[2, 6, 4, 3, 1] # Even Elements
[8, 2, 6, 4] # Odd Elements
[3, 1]
This is the most efficient way of filtering an array and it’s also the most Pythonic one.
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Linerswill 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 tips and tricks, regular expressions, machine learning, core data science topics, and 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.
Problem: Given a JSON object. How to pretty print it from the shell/terminal/command line using a Python one-liner?
Minimal Example: You have given the following JSON object:
{"Alice": "24", "Bob": "28"}
And you want to get the following print output:
{ "Alice": "24", "Bob": "28"
}
How to accomplish this using a Python one-liner?
Method 0: Python Program + json.dump
The default way to accomplish this in a Python script is to import the json library to solve the issue:
Exercise: Execute the script. What’s the output? Now change the number of indentation spaces to 2!
However, what if you want to run this from your operating system terminal as a one-liner command? Let’s dive into the four best ways!
Method 1: Terminal / Shell / Command Line with Echo + Pipe + json.tool
The echo command prints the JSON to the standard output. This is then piped as standard input to the json.tool program that pretty prints the JSON object to the standard output:
The pipe operator | redirects the output to the standard input of the Python script.
Method 2: Use a File as Input with json.tool
An alternative is the simple:
python -m json.tool file.json
This method is best if you have stored your JSON object in the file.json file. If the file contains the same data, the output is the same, too:
{ "Alice": "24", "Bob": "28"
}
Method 3: Use Web Resource with json.tool
If your JSON file resides on a given URL https://example.com, you’ll best use the following one-liner:
curl https://example.com/ | python -m json.tool
Again, assuming the same JSON object residing on the server, the output is the same:
{ "Alice": "24", "Bob": "28"
}
Method 4: Use jq
This is the simplest way but it assumes that you have the jq program installed on your machine. You can download jq here and also read about the excellent quick-start resources here.
Let’s dive into the code you can run in your shell:
The <<< operator passes the string on the right to the standard input of the command on the left. You can learn more about this special pipe operator in this SO thread.
While this method is not a Python script, it still works beautifully when executed from a Linux or MacOS shell or the Windows Powershell / command line.
Python One-Liners Book
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Linerswill 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 tips and tricks, regular expressions, machine learning, core data science topics, and 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.
Summary: To print without the newline character in Python 3, set the end argument in the print() function to the empty string or the single whitespace character. This ensures that there won’t be a newline in the standard output after each execution of print(). Alternatively, unpack the iterable into the print() function to avoid the use of multiple print() statements: print(*iter).
Let’s go over this problem and these two solutions step-by-step.
Problem: How to use the print() function without printing an implicit newline character to the Python shell?
Example: Say, you want to use the print() function within a for loop—but you don’t want to see multiple newlines between the printed output:
for i in range(1,5): print(i)
The default standard output is the following:
1
2
3
4
But you want to get the following output in a single line of Python code.
1 2 3 4
How to accomplish this in Python 3?
Solution: I’ll give you the quick solution in an interactive Python shell here:
By reading on, you’ll understand how this works and become a better coder in the process.
Let’s have a quick recap of the Python print() function!
Python Print Function – Quick Start Guide
There are two little-used arguments of the print function in Python.
The argument sep indicates the separator which is printed between the objects.
The argument end defines what comes at the end of each line.
a = 'hello'
b = 'world' print(a, b, sep=' Python ', end='!')
Try it yourself in our interactive code shell:
Exercise: Click “Run” to execute the shell and see the output. What has changed?
Solution 1: End Argument of Print Function
Having studied this short guide, you can now see how to solve the problem:
To print the output of the for loop to a single line, you need to define the end argument of the print function to be something different than the default newline character. In our example, we want to use the empty space after each string we pass into the print() function. Here’s how you accomplish this:
for i in range(1,5): print(i, end=' ')
The shell output concentrates on a single line:
1 2 3 4
By defining the end argument, you can customize the output to your problem.
Solution 2: Unpacking
However, there’s an even more advanced solution that’s more concise and more Pythonic. It makes use of the unpacking feature in Python.
print(*range(1,5))
# 1 2 3 4
The asterisk prefix * before the range(1,5) unpacks all values in the rangeiterable into the print function. This way, it becomes similar to the function execution print(1, 2, 3, 4) with comma-separated arguments. You can use an arbitrary number of arguments in the print() function.
Per default, Python will print these arguments with an empty space in between. If you want to customize this separator string, you can use the sep argument as you’ve learned above.
How to Print a List?
Do you want to print a list to the shell? Just follow these simple steps:
Pass a list as an input to the print() function in Python.
Use the asterisk operator * in front of the list to “unpack” the list into the print function.
Use the sep argument to define how to separate two list elements visually.
Here’s the code:
# Create the Python List
lst = [1, 2, 3, 4, 5] # Use three underscores as separator
print(*lst, sep='___')
# 1___2___3___4___5 # Use an arrow as separator
print(*lst, sep='-->')
# 1-->2-->3-->4-->5
Try It Yourself in Our Interactive Code Shell:
This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives—and improve your Python skills in the process of doing so—read the following tutorial!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
How do the Boolean and and or operators work in the context of Python one-liners?
You may know the standard use of the logical operators applied to Boolean values:
>>> True and False
False
>>> False or True
True
But there’s more to these operators that only experts in the art of writing concise Python one-liners know.
For instance, the following use of the or operator applied to non-Boolean values is little known:
>>> 'hello' or 42 'hello'
>>> [] or 42
42
Similarly, the following use of the and operator often causes confusion in readers of advanced Python one-liners:
>>> 'hello' and 42
42
>>> [] and 42
[]
How do the and and or operator work when applied to non-Boolean operands?
To understand what is going on, you need to look at the definitions of the Boolean operators:
Operator
Description
a or b
Returns b if the expression a evaluates to False using implicit Boolean conversion. If the expression a evaluates to True, the expression a is returned.
a and b
Returns b if the expression a evaluates to True using implicit Boolean conversion. If the expression a evaluates to False, the expression a is returned.
Study these explanations thoroughly! The return value is of the same data type of the operands—they only return a Boolean value if the operands are already Boolean!
This optimization is called short-circuiting and it’s common practice in many programming languages. For example, it’s not necessary to evaluate the result of the second operand of an and operation if the first operand evaluates to False. The whole operation must evaluate to False in this case because the logical and only returns True if both operands are True.
Python goes one step further using the property of implicit Boolean conversion. Every object can be implicitly converted to a Boolean value. That’s why you see code like this:
l = []
if l: print('hi')
else: print('bye')
# bye
You pass a list into the if condition. Python then converts the list to a Boolean value to determine which branch to visit next. The empty list evaluates to False. All other lists evaluate to True, so the result is bye.
Together, short circuiting and implicit Boolean conversion allow the logical operators and and or to be applied to any two Python objects as operands. The return value always is one of the two operands using the short circuiting rules described in the table.
Try it yourself in our interactive code shell:
Exercise: Guess the output! Then check if you were right! Create your own crazy operands and evaluate them by executing the code in your browser.
Python One-Liners Book
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Linerswill 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 tips and tricks, regular expressions, machine learning, core data science topics, and 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.
This interesting code snippet was brought to my attention by Finxter reader Albrecht.
a, b = 250, 250
for i in range(250, 260): if a is not b: break a += 1 b += 1
print(a)
# What's the output of this code snippet?
You’d guess that the for loop goes from i=250 to i=259, each time incrementing a and b. As Python creates one integer object to which both names refer, the command a is not b should always be False. Thus, the result is a=259, right?
WRONG!!! $%&&%$
Try it yourself in our interactive code shell:
Exercise: Run the code and check the result. Did you expect this?
If you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory.
“The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.”
You can visualize the code execution in this interactive memory visualizer:
Exercise: Click next until you see the result. How many integers are in memory?
Let’s quickly examine the meaning of “is” in Python.
The is operator
The is operator checks if two variable names point to the same object in memory:
>>> a = "hello"
>>> b = "hello"
>>> a is b
True
Both variables a and b point to the string "hello". Python doesn’t store the same string twice but creates it only once in memory. This saves memory and makes Python faster and more efficient. And it’s not a problem because strings are immutable — so one variable cannot “overshadow” a string object of another variable.
Note that we can use the id() function to check an integer representation of the memory address:
>>> a = "hello"
>>> b = "hello"
>>> id(a)
1505840752992
>>> id(b)
1505840752992
They both point to the same location in memory! Therefore, the is operator returns True!
Small Integer Caching
Again, if you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory. But if we create an integer object that does not fall into this range, Python may return a new integer object with the same value.
If we now check a is not b, Python will give us the correct result True.
In fact, this leads to the strange behavior of the C implementation of Python 3:
>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
Therefore, you should always compare integers by using the == operator in Python. This ensures that Python performs a semantic comparison, and not a mere memory address comparison:
>>> a = 256
>>> b = 256
>>> a == b
True
>>> a = 257
>>> b = 257
>>> a == b
True
What can you learn from this? Implementation details matter!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
URL encoding “is a method to encode information in a Uniform Resource Identifier (URI)”. It is also called Percent-encoding because percentage symbols are used to encode certain reserved characters:
!
#
$
%
&
'
(
)
*
+
,
/
:
;
=
?
@
[
]
%21
%23
%24
%25
%26
%27
%28
%29
%2A
%2B
%2C
%2F
%3A
%3B
%3D
%3F
%40
%5B
%5D
This article collects various ways to decode an URL encoded string. Let’s get started!
Python 2
$ alias urldecode='python -c "import sys, urllib as ul; \ print ul.unquote_plus(sys.argv[1])"' $ alias urlencode='python -c "import sys, urllib as ul; \ print ul.quote_plus(sys.argv[1])"'
urlencode() { local length="${#1}" for (( i = 0; i < length; i++ )); do local c="${1:i:1}" case $c in [a-zA-Z0-9.~_-]) printf "$c" ;; *) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done esac
done
}
If you run Python 3 on your system (like most people would), use the alternative function urllib.parse.unquote. To check your version, visit this article.
To break one line into multiple lines in Python, use an opening parenthesis in the line you want to break. Now, Python expects the closing parenthesis in one of the next lines and the expression is evaluated across line boundaries. As an alternative, you can also use the backslash \ just in front of the line break to escape the newline character.
Problem: Given a long line of Python code. How to break it into multiple lines?
There are multiple ways of breaking this into multiple lines. Let’s get an overview first:
Exercise: Run the code. What’s the output? Modify Method 3 and write it as a one-liner again!
We now dive into each of those methods.
Method 1: Implicit Line Continuation — Use Parentheses to Avoid Line Break Backslashes
The PEP 8 – Style Guide argues that the best way to break long lines into multiple lines of code is to use implicit line continuation by using parentheses. An opening parenthesis signals to Python that the expression has not finished, yet. So, the Python interpreter keeps looking in the following line to close the currently open expression. This way, you can rewrite any Python one-liner to a multi-liner just by using one or more pairs of parentheses.
Here’s the original statement from the PEP 8 style guide (emphasis by me):
The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.
Do you need an example for implicit line continuation? Here it is:
# Long Line
a = list(zip(['Alice', 'Bob', 'Liz', 'Ann'], [18, 24, 19, 16])) # Implicit Line Continuation
b = list(zip(['Alice', 'Bob', 'Liz', 'Ann'], [18, 24, 19, 16])) print(a)
# [('Alice', 18), ('Bob', 24), ('Liz', 19), ('Ann', 16)] print(b)
# [('Alice', 18), ('Bob', 24), ('Liz', 19), ('Ann', 16)]
The long line a = list(zip(['Alice', 'Bob', 'Liz', 'Ann'], [18, 24, 19, 16]))zips together two lists to obtain the result [('Alice', 18), ('Bob', 24), ('Liz', 19), ('Ann', 16)]. You can rewrite this into multiple lines by using the existing parentheses. Note that it is good style to hierarchically align the lines in Python. In our example, the two lists are aligned when creating the multi-line statement to assign the variable b.
Remember: you can always break lines if an opened bracket, parenthesis, or bracelet has not been closed!
Method 2: Explicit Line Continuation — Use the Line Break Backslash \
However, what if you don’t want to introduce new parentheses into your expression? Can you still break up a one-liner into multiple lines?
The answer is: yes! Just use the line break backslash \ which you may call explicit line continuation. With the backslash, you can break at any position in your expression. Technically, the backslash character “escapes” the newline character that follows immediately afterwards. By escaping the newline character, it loses its meaning and Python simply ignores it. This way, you don’t have to introduce any new parentheses.
Here’s a minimal example that shows the flexibility with which you can break new lines this way:
To solve this readability problem, mathematicians and their publishers follow the opposite convention.
Donald Knuth explains the traditional rule in his Computers and Typesetting series: “Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations”[3].
You can do both in Python but you should prefer the latter to improve readability of your code!
Method 3: Break a String by Using a Multi-Line String with Triple Quotes
Example: Say, you have the following long string from Romeo and Juliet:
s = 'Mistress! what, mistress! Juliet! fast, I warrant her, she:\n Why, lamb! why, lady! fie, you slug-a-bed!\n Why, love, I say! madam! sweet-heart! why, bride!\n What, not a word? you take your pennyworths now;\n Sleep for a week; for the next night, I warrant'
Note the newline character in the string: it’s really a multip-line string! Can you rewrite this into multiple lines to improve readability?
You can restructure strings by using the triple quotes that allows you to define a multi-line string without the newline character '\n' in the string. This significantly improves readability of multi-line strings! Here’s an example:
s1 = 'Mistress! what, mistress! Juliet! fast, I warrant her, she:\n Why, lamb! why, lady! fie, you slug-a-bed!\n Why, love, I say! madam! sweet-heart! why, bride!\n What, not a word? you take your pennyworths now;\n Sleep for a week; for the next night, I warrant' # MULTI-LINE
s2 = '''Mistress! what, mistress! Juliet! fast, I warrant her, she: Why, lamb! why, lady! fie, you slug-a-bed! Why, love, I say! madam! sweet-heart! why, bride! What, not a word? you take your pennyworths now; Sleep for a week; for the next night, I warrant''' print(s1 == s2)
# True
These are two ways of defining the same string. If you compare them, the result is True. However, the second way is far more readable and should be preferred!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
In this article, you’ll learn about the profound impact of the 80/20 principle on your life as a programmer. It’s based on a first draft of a chapter from my upcoming book “From 1 to 0: A Minimalistic Approach to Programming”.
The 80/20 principle has many names but the second most famous is the Pareto principle, named after his discoverer Vilfredo Pareto. So, how does the principle work and why should you care?
Pareto Principle Basics
The principle says that the majority of effects come from the minority of causes. For example, the majority of income is earned by the minority of people, the majority of innovations come from the minority of researchers, the majority of books is written by the minority of authors, the majority of the sales come from a minority of the clients, and the majority of goals are shot by the minority of soccer players.
Most likely, you’ve already heard about the 80/20 principle—it’s everywhere in personal productivity literature. The reason for its popularity is two-fold. First, the principle allows you to be lazy and productive at the same time—if you can figure out the things that matter and focus on those relentlessly. Second, the principle is observable everywhere in the real world. It’s very difficult to even come up with some phenomenon where the effects come equally from the causes. Go ahead and try to find some examples of 50/50 distributions where 50% of the effects come from 50% of causes! Sure, the distribution is not always 80/20. The concrete numbers can change to 70/30, 90/10, or even 95/5. However, the distribution is always heavily skewed towards the minority of the causes that produce the majority of effects.
Here’s an example of a Pareto distribution:
Figure: Example of a Pareto distribution: the causes are ordered according to the results they produce.
You can see the mathematical plot of a Pareto distribution that plots the generalized results against the causes—assuming the causes are ordered according to the results they produce.
Application Software Optimization
The following figure shows the application of the Pareto distribution to a software project: the minority of the code is responsible for the majority of the runtime. The x-axis shows code functions sorted by the runtime they incur. The y-axis shows the runtime of those code functions. The units don’t really matter here, but you should realize that the shaded area dominates the overall area under the plot. Most code functions contribute much less to the overall runtime than a few selected code functions. Spending a lot of time optimizing the “trivial many” barely produces any improvement of the overall runtime.
Figure: Example of a Pareto distribution in software engineering: most functions contribute little to the overall runtime but some functions contribute heavily.
While the principle is easily understandable, most people don’t intuitively understand the relevance of the principle in their own lives. How can you make use of the principle to get more done in less time?
Few people know that the principle was successfully employed by large computing companies such as IBM, Microsoft, and Apple to build computers that feel much faster and to create a user experience that has previously been unheard of. How did they do this? They channeled their focus on the “Top 20%”—by repeatedly optimizing the 20% of the code that was executed most often by the average user. Not all code is created equal. A minority of code has a dominating impact on the user experience while much of the code has little impact on it. For example, you double-click on an icon multiple times per day—programs should load very fast for great user experience—but you change the access rights of a file only seldomly, if at all. The 80/20 principle tells you where to focus your optimization efforts!
Productivity
In fact, the 80/20 principle is a principle of focus. By focusing on the vital few rather than the trivial many, you can 10x, even 100x your productivity at work. Don’t believe me? Let’s calculate where these numbers come from, assuming an underlying 80/20 distribution.
Figure: The average output of the 20% top performers is 16x the average output of the 80% bottom performers.
The real world tells us that a minority of people produces the majority of results. This fundamental principle is observable in a wide variety of different applications. Let’s plug in some numbers to get an intuition how large the performance difference is. For instance, let’s use the conservative 80/20 parameters: 80% of the results come from 20% of the people. In some fields (like programming), the distribution is probably much more skewed.
The previous figure shows that in a company of 10 persons, only two persons produce 80% of the results while eight produce 20% of the results—a direct consequence of the 80/20 principle.  If you divide 80% by two, you get an average output of 40% per top-performing person in the company. At the same time, if you divide the 20% of results generated by the eight persons, you obtain an average of 2.5% per bottom-performing person. The difference in performance is 16x!
And note that this is not a theoretical difference that could be achieved under some impractical settings—this 16x difference in average performance is already a fact in millions of organizations throughout the world.
The performance difference exists: there are two persons in your organization who create more than 10x higher output than a “normal” employee. The question is: how can you become one of those two? Or, to formulate it more generally: how can you “move to the left” on the Pareto distribution curve in your organization (see figure)?
Figure: To create more output, you need to constantly move to the left of the curve.
On the y-axis in our 80/20 world, I used the label “Output” to keep it general. You may want to optimize for income (20% of the people earn 80% of the incomes). You may want to optimize for happiness (20% of the people enjoy 80% of the happiness at work). You may want to optimize for monthly active users (20% of the websites have 80% of the monthly active users). You may want to optimize for book sales (20% of the books receive 80% of the sales). Or you may want to optimize for citations (20% of the researchers receive 80% of the citations).
This shows a critical take-away that follows from the 80/20 principle: be clear what you want to optimize.
Success Metrics
Let’s say, we want to optimize for income as a proxy for happiness. How can you move to the left in the Pareto curve?
Now, you’re leaving exact science because you need to find the reasons why some people succeed: which of their expertise generates most of the success? You need to find a plausible, simplifying argument in your specific industry and develop actionable success metrics you can control. If you do more of them, you’ll become more successful. If you do less of them, you’ll become less successful. The tricky thing is that the success metrics are different in most fields. In fact, the 80/20 principle also applies to success metrics: some success metrics have a dominating impact on your performance in a field, while others barely matter at all.
For example, when working as a doctoral researcher, I soon realized that it’s all about citations. The more citations you have as a researcher, the more credibility, visibility, and opportunities you’ll experience in science. How can we influence citations (“today, I’ll increase the number of citations” is hardly an actionable success metric)? Citations come from high-class papers. If you publish more high-class papers, you’ll receive more citations. So, writing high-class papers is the most important activity for most scientists. However, many researchers get distracted by secondary activities such as preparing presentations, organizing, teaching, drinking coffee, the most successful researchers focus heavily on generating a maximal number of high-quality papers. For researchers, the Pareto distribution of the success metric for researchers may look like this:
Figure: Success metric in research: number of words written towards high-class paper.
By replacing the generalized “Output” with the new success metric “Number of words written towards high-class paper”, you have gained crystal clear insight into what you must do every day to push towards the left in research. If you write more words today, you’ll publish your next high-class paper sooner, receive more citations faster, grow your scientific footprint, and become a more successful scientist as a result. The beauty of this 80/20 approach is that it allows you to find your focus. Everything else doesn’t matter. You can relax, lean back, and focus on the things that are very important. You can spend less time on all the different tasks. You don’t have to die the death of a thousand cuts. You can be lazy with all activities but one: writing papers. You can blend most things out, ignore emails, don’t go to meetings that don’t push you to more papers, be lazy in all the other activities. Say, you work 8h per day and you spread your day into eight one-hour activities. After completing the success metric exercise, you realize that you can skip two 1h activities per day and reduce four of them by half to complete them in half an hour instead of an hour (being less perfectionistic). You have saved 4h per day but you still accomplish, say, 80% of your results. Now, you invest 2h into writing more words towards high-class papers per day. Within a few months, you’ll have submitted an extra paper and over time, you’ll submit much more papers than any other of your colleague. You work only 6h per day and you generate imperfect quality in most of your work tasks. But you shine on where it matters: you submit more research papers than anyone else in your environment. As a result, you’ll soon be one of the top 20% of researchers. You generate more with less.
This is the power of 80/20 thinking: you invest resources where they have the highest leverage. You create more results by investing less time, effort, money. You become lazy in most things in life. But you focus some of the saved time, energy, and money on the ones that are wildly important. Instead of becoming a “Jack of all trades, master of none”, you become a one-trick pony. You heavily focus on the vital few and ignore the trivial many. You lead a less stressful life, but you enjoy more fruits from your invested labor, efforts, time, and money.
Pareto Implications for Coders
Let’s consider another example: if you’re reading this book, you’re a programmer. In programming, the results are much more heavily skewed towards the top than in most other fields. Instead of 80/20, the distribution often looks more like 90/10. Bill Gates said that a “great lathe operator commands several times the wage of an average lathe operator, but a great writer of software code is worth 10,000 times the price of an average software writer”. Bill Gates has overseen hundreds of thousands of programmers and software developers and if he makes this statement, it must have some merit. Interestingly, the difference is not 16x like you’ve seen previously. The difference between a great and an average software writer is 10,000x! How can this be? Well, here are a number of reasons why this extreme Pareto distribution holds especially in the software world:
A great programmer can solve some problems that the average programmer simply cannot solve. In some instances, this makes him infinitely-times more productive.
A great programmer can write code that is 10,000 times faster than the code of an average programmer. This can make or break the viability of a whole product line of a billion-dollar company.
A great programmer will write code that has fewer bugs. Think about the effect of a single security bug on Microsoft’s reputation and brand!
A great programmer will write code that is easier to extend which may improve the productivity of thousands of developers that work on his code at a later stage of the software developing process.
A great programmer will think out of the box and find creative solutions to circumvent costly development efforts and help focus on the most important things.
Each of the previously stated arguments show why a great software developer can be 10,000 times more productive. In practice, a combination of those factors is at play so that the difference can be even higher.
The key question is: how do you become a great programmer? Because if you can become a great programmer, you’ll always have much more work than you can handle, and the most successful companies in the world—Google, Facebook, Amazon, Apple, and Microsoft—will be happy to pay you big premiums for your services.
A Success Metric for Programmers
Unfortunately, the statement “become a great programmer” is not a success metric you can directly optimize—it’s a multi-dimensional problem. A great programmer can mean a lot of things. He or she understands code quickly, knows algorithms and data structures, knows different technologies and their strengths and weaknesses, can collaborate with other people, is communicative and creative, stays educated and knows about ways to organize the software development process, and possesses hundreds of soft- and hard-skills. But you cannot master all of those! If you don’t focus on the vital few, you’ll become enslaved by the trivial many. To become a great programmer, you must focus on the vital few. One of those vital few activities that will ensure that you become a better coder over time is the success metric “write more lines of code”. If you write more lines of code than your peers, you’ll become a better coder than most of your peers. It’s a simplification of the multi-dimensional problem but we simplified towards the vital few—by optimizing the proxy metric “write more lines of code”, we increased our odds of succeeding at the target metric “become a great writer of software code” (see figure).
Figure: Success metric in programming: number of lines of code written.
By writing more code, you create a self-reinforcing feedback loop. By writing more code, you begin to understand code better. You talk and behave more like an expert coder. You attract better coders and more challenging programming tasks, so you write more code and become even better. You get paid more and more per line of code you write, thus, it makes economic sense to write more code instead of doing housework or doing tedious non-programming tasks at work. You or your company outsource everything else. The more you code, the more successful you’ll become. Here you have the 80/20 activity you can follow every day: track the number of lines you code every day and optimize it. Make it a game to at least match your average every day. If you code more, you’ll ultimately join the top 10% of coders with income levels far above six figures.
Relationship Between Focus and the Pareto Distribution
A closely related topic I want to discuss is focus. The 80/20 principle explains why focus is so powerful. Let’s dive into the argument!
Consider the Pareto distribution in the next figure that shows the percentage improvement of moving towards the top of the distribution. Alice is the fifth most productive person in the organization. If she just overtakes one person in the organization, thereby becoming the fourth most productive person, she’d increase her output (e.g., salary) by 10%. If she moves one step further, her output increases by additional 20%. Think about this: even if she could keep increasing her income by 10% repeatedly, this would be great because she would experience superlinear growth. But in a Pareto-distribution, the growth per rank explodes. This is why even small increases of productivity can result in big increases of income. If you can move towards the top 10% in any Pareto distribution, you’ll be a wildly successful person with massive results in your life. It doesn’t matter if you’re a golfer, Poker player, programmer, or machine learning engineer. Increasing your productivity leads to super-linear improvements of your income, happiness, and joy at work. Some call this phenomenon: the winner takes it all.
Figure: Disproportional benefit of improving your rank in a Pareto distribution.
That’s why it doesn’t pay not to focus: if you don’t focus, you participate in many pareto distributions. Let’s consider the following graphic of two persons: Alice and Bob. Both have three units learning efforts every day. Alice focuses on one thing: programming. She’s neither a good chess player, nor a good golfer, nor good at politics. She just spends three units of efforts in learning to code. Bob spreads his focus to multiple disciplines. He spends one unit of time polishing his chess skills, one unit training his programming skills, and one unit training his politics skills. As a result, he’s reached average skills and output in each of the three areas. However, due to the nature of the Pareto distribution to disproportionally reward the winners in any Pareto distribution, Alice collects more total output (e.g., income or happiness) than Bob through her focusing strategy.
Figure: Non-linearity of rank output – A strategic explanation attempt for the power of focus.
Note that this holds true not only across broad and independent areas such as programming, chess, and politics. It also applies within narrow areas such as programming. For instance, Bob may spend his time reading three general books (let’s call them: Introduction to Python, Introduction to C++, and Introduction to Java) while Alice reads three books diving deep into Python (let’s call them: Introduction to Python, Introduction to Machine Learning with Python, and Machine Learning for Experts). As a result, Alice will focus in becoming a machine learning expert and can demand a higher salary for her specialized skill set.
Github Contributions
Another example for a Pareto distribution gone extreme can be seen in contributions to Github repositories. There’s scientific evidence that contributions to open-source projects are Pareto distributed.[1] Let’s consider a wildly repository for machine learning computations in Python: TensorFlow. Here are the top seven contributors to the Github repository:
Here’s the table showing the same data numerically:
Contributor
Commits
tensorflower-gardener
21426
yongtang
1251
mrry
1120
gunan
1091
River707
868
benoitsteiner
838
sanjoy
795
The user tensorflow-gardener contributed more than 20% of the 93,000 commits to this repository. Given that there are thousands of contributors, the distribution is much more extreme than the 80/20 distribution. The reason for this extreme skewness is that the contributor tensorflow-gardener consists of a team of coders at Google who create and maintain this repository. The interesting observation, however, is that the top contributors are extremely successful programmers with impressive track records working for some of the most successful companies in the world. You can check them out publicly on the Github page. Whether they became successful before or after they generated a large amount of commits to the open-source repository is a mere theoretical discussion. For all practical matters, you should start your success habit write more lines of code every day now. There’s nothing stopping you from becoming #2 on the TensorFlow repository – by committing valuable code to the TensorFlow repository 2-3 times per day for the next 2-3 years. If you persist, you can join the ranks of the most successful coders on earth – by choosing one powerful habit and sticking to it for three years!
The underlying driver of excellence is to leverage the 80/20 principle on multiple fronts: First, you focus on the minority of activities that are most able to push you to success in your profession. Second, you do more of these activities than 80% of the professionals in your industry so that you belong to the top 20% of the professionals regarding these selected activities. By chaining these two Pareto distributions—select the top 20% of activities and join the top 20% in terms of activity execution quantity—you maximally leverage your resources and you’ll become an unstoppable force in your industry. Are you prepared to take a ride to the moon?
Programmer Net Worth
Sure enough, the net worth of programmers is also Pareto distributed. For privacy reasons, it’s hard to get data about individual’s net worth but one page[3] shows the self-reported net worth of computer programmers. Although the data may be noisy, it shows the characteristic skewness of real-world Pareto distributions:
Figure: Self-reported net worth of 60 programmers.
In fact, the curve is likely to be even more skewed in the real-world because there are many billionaire programmers who’ve initiated software services used by billions of people –
Mark Zuckerberg, Bill Gates, Elon Musk, Steve Wozniak come to mind. Each of those tech geniuses created the prototypes of their services themselves laying hand on the source code. The number of software millionaires is significant.