Posted on Leave a comment

HTML Parsing using Python and LXML

In this article, you’ll learn the basics of parsing an HTML document using Python and the LXML library.

Introduction

Data is the most important ingredient in programming. It comes in all shapes and forms. Sometimes it is placed inside documents such as CSV or JSON, but sometimes it is stored on the internet or in databases. Some of it is stored/transferred or processed through the XML format, which is in many ways similar to HTML format, yet its purpose is to transfer and store data, unlike HTML, whose main purpose is to display the data. On top of that, the way of writing HTML and XML is similar. Despite the differences and similarities, they supplement each other very well.

Both Xpath and XML are engineered by the same company W3C, which imposes that Xpath is the most compatible Python module to be used for parsing the XML documents. Since one of the programing principals which would push you towards the programming success is to “not reinvent the wheel”, we are going to refer to the W3C (https://www.w3.org/) consortium document and sources in regarding the syntax and operators on our examples to bring the concept of XPath closer to the people wishing to understand it better and use it on real-life problems. 

The IT industry has accepted the XML way of transferring data as one of its principles. Imagine if one of your tasks was to gather information from the internet? Copying and pasting are one of the simplest tools to use (as it is regularly used by programmers as well); it might only lead us to gather some simple data from the web, although the process might get painfully repetitive. Yet, in case if we have more robust data, or more web pages to gather the data from, we might be inclined to use more advanced Python packages to automate our data gathering.

Before we start looking into scraping tools and strategies, it is good to know that scraping might not be legal in all cases, therefore it is highly suggested that we look at the terms of service of a particular web site, or copyright law regarding the region in which the web site operates.

For purposes of harvesting the web data, we will be using several Python libraries that allow us to do just that. The first of them is the requests module. What it does is that it sends the HTTP requests, which returns us the response object. It only used if urge to scrape the content from the internet. If we try to parse the static XML file it would not be necessary.

There are many parsing modules. LXML, Scrapy and BeautifulSoup are some of them. To tell which one is better is often neglected since their size and functionality differs from one another. For example, BeautifulSoup is more complex and serves you with more functionality, but LXML and Scrapy comes lightweight and can help you traversing through the documents using XPath and CSS selectors.

There are certain pitfalls when trying to travel through the document using XPath. Common mistake when trying to parse the XML by using XPath notation is that many people try to use the BeautifulSoup library. In fact that is not possible since it does not contain the XPath traversing methods. For those purposes we shall use the LXML library.

The requests library is used in case we want to download a HTML mark-up from the particular web site.

The first step would be to install the necessary packages. Trough pip install notation all of the modules above could be installed rather easily.

Necessary steps:

  1. pip install lxml (xpath module is a part of lxml library)
  2. pip install requests (in case the content is on a web page)

The best way to explain the XML parsing is to picture it through the examples.

The first step would be to install the necessary modules. Trough pip install notation all of the modules above could be installed rather easily.

What is the XPath?

The structure of XML and HTML documents is structurally composed of the nodes (or knots of some sort), which is a broader picture that represents the family tree-like structure. The roof instance, or the original ancestor in each tree, is called the root node, and it has no superior nodes to itself. Subordinate nodes are in that sense respectively called children or siblings, which are the elements at the same level as the children. The other terms used in navigating and traversing trough the tree are the ancestors and descendants, which in essence reflect the node relationship the same way we reflect it in real-world family tree examples. 

XPath is a query language that helps us navigate and select the node elements within a node tree. In essence, it is a step map that we need to make to reach certain elements in the tree.  The single parts of this step map are called the location steps, and each of these steps would lead us to a certain part of the document.

The terminology used for orientation along the axis (with regards to the current node) is very intuitive since it uses regular English expressions related to real-life family tree relationships.

XPath Selector

XPath selector is the condition using which we could navigate through an XML document. It describes relationships as a hierarchical order of the instances included in our path.  By combining different segment of XML syntax it helps us traverse through to the desired parts of the document. The selector is a part of the XPath query language. By simply adding different criteria, the XPath selector would lead us to different elements in the document tree.  The best way to learn the XPath selector syntax and operators is to implement it on an example. In order to know how to configure the XPath selector, it is essential to know the XPath syntax. XPath selector is compiled using an etree or HTML module which is included within the LXML package. The difference is only if we are parsing the XML document or HTML.

The selector works similarly as a find method with where it allows you to select a relative path of the element rather than the absolute one, which makes the whole traversing less prone to errors in case the absolute path gets too complicated.

XPath Syntax

XPath syntax could be divided into several groups. To have an exact grasp of the material presented we are going to apply further listed expressions and functions on our sample document, which would be listed below. In this learning session, we are going to use a web site dedicated to scraping exercises.

Node selection:

Expression Description
nodename Selects all nodes with the name “nodename
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are.
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

Using “..”  and “.” we can direct and switch levels as we desire. Two dot notations would lead us from wherever we are to our parent element, whereas the one dot notations would point us to the current node. 

The way that we travel from the “context node” (our reference node), which is the milestone of our search, is called “axes”, and it is noted with double slash //. What it does is that it starts traversing from the first instance of the given node. This way of path selection is called the “relative path selection”. To be certain that the // (empty tag) expression would work, it must precede an asterisk (*) or the name tag. Trough inspecting the element and copying its XPath value we are getting the absolute path.

XPath Functions and Operators

here are 6 common operators which are used inside the XPath query. Operators are noted the same way as in plain Python and serve the same purpose. The functions are meant to aid the search of desired elements or their content.

Path Expression Result
= Equal to
!= Not equal to
Is greater than
Is less than
=> Is greater or equal to
=< Is less or equal to

To add more functionality to our XPath expression we can use some of LXML library functions. Everything that is written in-between the “[]” is called a predicate and it is used to closer describe the search path. Most frequently used functions are contains() and starts-with(). Those functions and their results would be displayed in the table below.

Going Up and Down the Axis

The conventional syntax used to traverse up and down the XPath axes is ElementName::axis.

To reach the elements placed above or below our current axes, we might use some of the following axes.

Up the axes Examples  
ancestor //ul/li/a[contains(@href, 'categ')]/ancestor::node()  
parent //ul/li/a[contains(@href, 'categ')]/parent::node()  
preceding //ul/li/a[contains(@href, 'categ')]/preceding::div  
preceding-sibling //a[contains(@href, 'categ')]/preceding-sibling::*  
Down the axes Examples  
descendant //a[starts-with(@href, 'catalogue')]/descendant::*  
following /html/body/div/div/ul/li[1]/a  
following-sibling /html/body/div/div/ul/li[1]/a/following::li  
child //div/div/section/div[2]/ol/li[1]/article/h3/child::*  

A Simple Example

The goal of this scraping exercise is to scrape all the book genres placed at the left-hand side of the web site. It almost necessary to see the page source and to inspect some of the elements which are we aiming to scrape.

from lxml import html
import requests url = 'http://books.toscrape.com/' # downloading the web page by making a request objec
res = requests.get(url) # making a tree object
tree = html.fromstring(res.text) # navingating the tree object using the XPath
book_genres = tree.xpath("//ul/li/a[contains(@href, 'categ')]/text()")[0:60] # since the result is the list object, we iterate the elements,
# of the list by making a simple for loop
for book_genre in book_genres: print (book_genre.strip())

Resources:

  1. https://lxml.de/
  2. https://scrapinghub.github.io/xpath-playground/
  3. https://2.python-requests.org/en/master/
  4. ‘http://books.toscrape.com/
Posted on Leave a comment

Python One Line If Without Else

Crafting beautiful Python one-liners is as much an art as it is a science. In this tutorial, you’ll learn how to compress an if statement without an else branch into a single line of Python code.

Problem: What’s the one-liner equivalent of the simple if statement without an else branch?

Here’s an example:

condition = True if condition: print('hi') # hi

You may want to (i) print something, (ii) assign a value to a variable, or (iii) append an element to a list if the condition holds.

In this article, I’ll show you four methods of how to accomplish this goal. All four methods are generally applicable—and you can easily customize them to your specific application.

Let’s have a quick overview of the four methods in our interactive code shell:

Exercise: Run the code for both True and False conditions. Are all methods semantically equivalent?

Method 1: One-Liner If Statement

The first is also the most straightforward method: if you want a one-liner without an else statement, just write the if statement in a single line! There are many tricks (like using the semicolon) that help you create one-liner statements. But for an if body with only one statement, it’s just as simple as avoiding the line break.

condition = True # Method 1: One-Liner If
if condition: print('hi')
# hi

This method is perfectly valid and you could see it in practice. Yet, I have to mention that it “violates” the PEP8 standard (multiple statements in a single line). Therefore, you shouldn’t consider this to be Pythonic code (there are worse things under the sun though).

Method 2: Ternary with Throw-Away Else Branch

Sure, you can also use the ternary operator:

Python Ternary Operator

If you need a quick refresher on the ternary operator, check out my detailed blog article. The ternary operator is commonly used to conditionally assign values. But you can also throw away the return value by not assigning the result to any variable. In this case, it doesn’t matter if you use None or any other “dummy” return value as the result of the else branch:

condition = True # Method 2: Ternary with Dummy
print('hi') if condition else None

It’s readable, short, and concise and I like this (well, I may be a bit biased as author of the book Python One-Liners). Sure, people will ask why you didn’t write it in multiple lines. But where is the fun there?

Method 3: Ternary with Default Value for Assignment

If you need to assign a value conditionally to a variable, but you want to do so without an else branch, you can do the following:

condition = True # Method 3: Ternary with Dummy for Assignment
x = 42 if condition else None

If the condition does not hold, the “dummy” value None is assigned to the variable.

Method 4: Short Circuiting

This method I like most. It uses a Python optimization called “short circuiting” for Boolean operators: the logical and operator simply returns the second operand if the first is True. There’s no Boolean conversion of the second operand, it’s just returned as is.

If the first operand is False, the second operand is not even evaluated.

You can use this to conditionally execute the if branch print('hi') or any other code function.

condition = True # Method 4: Short circuiting
condition and print('hi')

There are two options:

  • condition == True: As the first operand is True, the second operand is returned. Thus, the statement print('hi') is executed and the string hi appears on the screen.
  • condition == False: As the first operand is False, the second operand is not even evaluated because the result of the logical and operation is False anyway. Thus, the statement print('hi') is never executed.

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Posted on Leave a comment

NumPy polymulx()

numpy.polynomial.polynomial.polymulx(c)

The numpy.polymulx function multiplies the polynomial c with a value x which is the independent variable.

Arguments Type Description
c array_like or poly1d object The input polynomials to be multiplied

The following table shows the return value of the function:

Type Description
Return Value ndarray or poly1d object The polynomial resulting from the multiplication of the inputs. If either inputs is a poly1d object, then the output is also a poly1d object. Otherwise, it is a 1D array of polynomial coefficients from highest to lowest degree.

Let’s dive into some examples to show how the function is used in practice:

Examples

import numpy as np
import numpy.polynomial.polynomial as poly print(poly.polymulx([0]) == [0])
print(poly.polymulx([1]) == [0, 1])
for i in range(1, 5): ser = [0]*i + [1] tgt = [0]*(i + 1) + [1] print(poly.polymulx(ser) == tgt) '''
[ True]
[ True True]
[ True True True]
[ True True True True]
[ True True True True True]
[ True True True True True True] '''

This function is inspired from this Github repository.

Any master coder has a “hands-on” mentality with a bias towards action. Try it yourself—play with the function in the following interactive code shell:

Exercise: Change the parameters of your polynomials and print them without the comparisons. Do you understand where they come from?

Master NumPy—and become a data science pro:

Coffee Break NumPy

Related Video

Posted on Leave a comment

numpy.char.capitalize

numpy.char.capitalize(a)

Original Documentation

Return a copy of a with only the first character of each element capitalized.

Calls str.capitalize element-wise.

For 8-bit strings, this method is locale-dependent.

Parameters:

  • a: array_like of str or unicode: Input array of strings to capitalize.

Returns:

  • out ndarray: Output array of str or unicode, depending on input types

See also

str.capitalize

Examples

Posted on Leave a comment

Python Ternary Elif

Summary: To use an elif branch in the ternary operator, use another ternary operator as the result of the else branch (nested ternary operator). The nested ternary operator x if c0 else y if c1 else z returns x if condition c0 is met, else if (elif) condition c1 is met, it returns y, else it returns z.

Python Ternary Elif

Problem: You may have seen the ternary operator x if c else y. Is there a similar ternary operator with an additional elif statement? In pseudocode, you want something like:

# Pseudocode
x if c elif y0 else y1

In other words: What’s the best way of extending the ternary operator to what you may call a “quaternary” operator?

Background: The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y.

Learn more about the ternary operator in our detailed blog article!

Example: Say, you want to write the following if-then-else condition in a single line of code:

>>> x = 42
>>> if x > 42:
>>> print("no")
>>> elif x == 42:
>>> print("yes")
>>> else:
>>> print("maybe")
yes

The elif branch wins: you print the output "yes" to the shell.

But how to do it in a single line of code? Just use the ternary operator with an elif statement won’t work (it’ll throw a syntax error):

Method: Nested Ternary Operator

The answer is simple: nest two ternary operators like so:

>>> print("no") if x > 42 else print("yes") if x == 42 else print("maybe")
yes

If the value x is larger than 42, we print “no” to the shell. Otherwise, we execute the remainder of the code (which is a ternary operator by itself). If the value x is equal to 42, we print “yes”, otherwise “maybe”.

So by nesting multiple ternary operators, we can greatly increase our Python one-liner power!

Try it yourself:

Exercise: Which method is more concise? Count the number of characters (or write a small script that does it for you ;))!

Python Ternary Multiple Elif

In the previous example, you’ve seen how a nested ternary operator semantically adds an elif branch. In theory, you can add an arbitrary number of elif branches by nesting more and more ternary operators:

# Method 1: If ... Elif ... Else
x = 42
if x > 42: y = 1
elif x == 42: y = 2
elif x == 12: y = 3
else: y = 4
print(y)
# 2 # Method 2: Nested Ternary Operator
y = 1 if x > 42 else 2 if x == 42 else 3 if x == 12 else 4
print(y)
# 2

However, readability suffers badly and you shouldn’t do anything of the sort. A simple mult-line if ... elif ... elif ... else statement is better!

Discussion

However, even if the nested ternary operator is more concise than an if-elif-else statement, it’s not recommended because of readability of your code. Most programmers don’t have any trouble understanding a simple if-elif-else statement. But a nested ternary operator is an advanced-level piece of Python code and especially beginners will struggle understanding it.

So, it’s great that you’ve expanded your One-Liner Superpower. But you should use it wisely!

Related Video: If-Then-Else in One Line of Python Code

Python One-Liners Book

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 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.

Get your Python One-Liners Now!!

Posted on Leave a comment

Python Ternary Multiple Lines

What if you have a ternary operator that’s very long?

var = 'I want to learn Python' if 42**2<166 else 'I want to learn Go programming'
print(var)
# I want to learn Go programming

Problem: How to write the ternary operator in multiple lines?

Short Recap: Ternary Operator

Ternary Operator: The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y.

Syntax: The three operands are written as x if c else y which reads as “return x if c else return y“. Let’s write this more intuitively as:

<OnTrue> if <Condition> else <OnFalse>
Operand Description
<OnTrue> The return expression of the operator in case the condition evaluates to True
<Condition> The condition that determines whether to return the <On True> or the <On False> branch.
<OnFalse> The return expression of the operator in case the condition evaluates to False
Operands of the Ternary Operator

Related article: For a full tutorial on the ternary operator, check out our detailed blog article.

Method: Parenthesis to Extend Logical Line Over Multiple Physical Lines

Solution: You can extend any logical line in Python over multiple physical lines by using the parenthesis.

var = 'I want to learn Python' if 42**2<166 else 'I want to learn Go programming'
print(var) var = ('I want to learn Python' if 42**2<166 else 'I want to learn Go programming')
print(var)
# I want to learn Go programming

This is the PEP8 standard way of breaking long lines—if you cannot do it in a more natural way (such as avoiding the ternary operator and using the if statement in this example).

Try it yourself:

Exercise: Write a nested ternary operator and break it into multiple lines!

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Posted on Leave a comment

Python Ternary — Tuple Syntax Hack

You may know the ternary operator x if c else y in Python which reads as “return x if c else return y“. But you may not know that you can also write the ternary operator in a shorter form as (y, x)[c]. Let’s dive into this concise way of hacking your own ternary operator!

Python Ternary Tuple Syntax

A shorthand form of the ternary operator is the following tuple syntax hack.

Syntax: You can use the tuple syntax (x, y)[c] consisting of a tuple (x, y) and a condition c enclosed in a square bracket. Here’s a more intuitive way to represent this tuple syntax.

(<OnFalse>, <OnTrue>)[<Condition>]
Operand Description
<OnTrue> The return expression of the operator in case the condition evaluates to True
<Condition> The condition that determines whether to return the <On True> or the <On False> branch.
<OnFalse> The return expression of the operator in case the condition evaluates to False
Tuple Syntax of the Ternary Operator

Exercise: Run the code and compare both ternary operators—the original and the tuple syntax hack.

In fact, the order of the <OnFalse> and <OnTrue> operands is just flipped when compared to the basic ternary operator. First, you have the branch that’s returned if the condition does NOT hold. Second, you run the branch that’s returned if the condition holds.

age = 17
print(('wtf', 'What?')[age<20]) 'What?'

The condition age<20 holds so the return value passed into the print() function is the <OnTrue> branch 'What?'. Don’t worry if this confuses you—you’re not alone. Let’s clarify why this tuple syntax works the way it does!

First, you create a tuple ('wtf', 'What?'). To access the first tuple value 'wtf', you’d use the standard indexing syntax ('wtf', 'What?')[0]. To access the second tuple value 'What?', you’d use the standard indexing syntax ('wtf', 'What?')[1].

Second, you create a condition age<20. You use this condition as the indexing value. You end up with either ('wtf', 'What?')[False] or ('wtf', 'What?')[True]. As you may know, the Booleans False and True are represented through integers 0 and 1 in Python. Thus, you get ('wtf', 'What?')[0] and ('wtf', 'What?')[1], respectively.

In other words: if your condition evaluates to False, you access the first tuple value. If your condition evaluates to True, you access the second tuple value.

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Posted on Leave a comment

Python One Line Ternary

Ternary (from Latin ternarius) is an adjective meaning “composed of three items”. (source) So, literally, the ternary operator in Python is composed of three operands.

Syntax: The three operands are written in an intuitive combination ... if ... else ....

<On True> if <Condition> else <On False>
Operand Description
<On True> The return expression of the operator in case the condition evaluates to True
<Condition> The condition that determines whether to return the <On True> or the <On False> branch.
<On False> The return expression of the operator in case the condition evaluates to False
Operands of the Ternary Operator

Let’s have a look at a minimum example in our interactive code shell:

Exercise: Run the code and input your age. What’s the output? Run the code again and try to change the output!

Let’s dive into the different variants of the Ternary operator in Python!

Python Ternary Examples

Let’s have a quick overview of a few examples on different methods to use the ternary operator:

age = 17 # Method 1: Basic Ternary
print('wtf' if age<20 else 'What?') 'wtf' # Method 2: Ternary Tuple
# (onFalse, onTrue) [condition]
print(('wtf', 'What?') [age<20]) 'What?' # Method 3: Ternary Dictionary
# Use Dictionary True/False values
print({True: 'wtf', False: 'What?'} [age<20]) 'wtf' # Method 4: Ternary Lambda
# Lambda function with 0 arguments
# Execute only one branch expression --> more efficient
print((lambda: 'wtf', lambda:'What?') [age<20]()) 'What?'

Some of them are pretty confusing, right? Stay with me for a moment because you’ll learn about each of those next! 🙂

Python Ternary Tuple

Python Ternary Dictionary

Python Ternary Lambda

Python Ternary Multiple Lines

Python Ternary Elif

Python Ternary Nested

Python Ternary Plot

Python Ternary Diagram

Python Ternary Evaluation Order

Python Ternary Try Except

Python Ternary Tree

Python Ternary in List Comprehension

Python Ternary Pep8 Pythonic

Python Ternary Can’t Assign to Conditional Expression

Python Ternary For Loop

Python Ternary Break

Python Ternary None

Python Ternary Multiple Conditions

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Python One-Liners Book

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 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.

Get your Python One-Liners Now!!

Posted on Leave a comment

Python One Line Quicksort

In this one-liner tutorial, you’ll learn about the popular sorting algorithm Quicksort. Surprisingly, a single line of Python code is all you need to write the Quicksort algorithm!

Problem: Given a list of numerical values (integer or float). Sort the list in a single line of Python code using the popular Quicksort algorithm!

Example: You have list [4, 2, 1, 42, 3]. You want to sort the list in ascending order to obtain the new list [1, 2, 3, 4, 42].

Short answer: The following one-liner solution sorts the list recursively using the Quicksort algorithm:

q = lambda l: q([x for x in l[1:] if x <= l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else []

You can try it yourself using the following interactive code shell:

Now, let’s dive into some details!

A Conceptual Introduction

The following introduction is based on my new book “Python One-Liners” (Amazon Link) that teaches you the power of the single line of code (use it wisely)!

Python One-Liners

Introduction: Quicksort is not only a popular question in many code interviews – asked by Google, Facebook, and Amazon – but also a practical sorting algorithm that is fast, concise, and readable. Because of its beauty, you won’t find many introduction to algorithm classes which don’t discuss the Quicksort algorithm.

Overview: Quicksort sorts a list by recursively dividing the big problem (sorting the list) into smaller problems (sorting two smaller lists) and combining the solutions from the smaller problems in a way that it solves the big problem. In order to solve each smaller problem, the same strategy is used recursively: the smaller problems are divided into even smaller subproblems, solved separately, and combined. Because of this strategy, Quicksort belongs to the class of “Divide and Conquer” algorithms.

Algorithm: The main idea of Quicksort is to select a pivot element and then placing all elements that are larger or equal than the pivot element to the right and all elements that are smaller than the pivot element to the left. Now, you have divided the big problem of sorting the list into two smaller subproblems: sorting the right and the left partition of the list. What you do now is to repeat this procedure recursively until you obtain a list with zero elements. This list is already sorted, so the recursion terminates.

The following Figure shows the Quicksort algorithm in action:

Figure: The Quicksort algorithm selects a pivot element, splits up the list into (i) an unsorted sublist with all elements that are smaller or equal than the pivot, and (ii) an unsorted sublist with all elements that are larger than the pivot. Next, the Quicksort algorithm is called recursively on the two unsorted sublists to sort them. As soon as the sublists contain maximally one element, they are sorted by definition – the recursion ends. At every recursion level, the three sublists (left, pivot, right) are concatenated before the resulting list is handed to the higher recursion level.

This brings us to the following code:

Python One-Liner Quicksort [Code]

Create a function q which implements the Quicksort algorithm in a single line of Python code – and thus sorts any argument given as a list of integers.

## The Data
unsorted = [33, 2, 3, 45, 6, 54, 33] ## The One-Liner
q = lambda l: q([x for x in l[1:] if x <= l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else [] ## The Result
print(q(unsorted))

Listing: One-liner solution for the Quicksort algorithm using recursion.

What is the output of this code? Let’s see…

Explanation Quicksort One-Liner

We have already discussed the recursive Quicksort algorithm above. The one-liner resembles exactly the discussed algorithm. First, we create a new lambda function q which takes only one list argument l.

The lambda function has the following structure:

lambda l: q(left) + pivot + q(right) if l else []

The lambda function returns the empty list [] in the recursion base case (that is – the list to be sorted is empty and, therefore, trivially sorted).

In any other case, it selects the pivot element as the first element of list l, divides all elements into two sublists (left and right) based on whether they are smaller or larger than the pivot. To achieve this, we use simple list comprehension.

As the two sublists are not necessarily sorted, we recursively execute the Quicksort algorithm on them. Finally, we combine all three lists and return the sorted list. Therefore, the result is:

## The Result
print(q(unsorted))
# [2, 3, 6, 33, 33, 45, 54]

Related: For an interactive experience of what you’ve just learned, check out our Instagram post about the Quicksort algorithm:

Related Resources:

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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!