Posted on Leave a comment

Python One Line Append

Do you want to one-linerize the append() method in Python? I feel you—writing short and concise one-liners can be an addiction! 🙂

This article will teach you all the ways to append one or more elements to a list in a single line of Python code!

Python List Append

Let’s quickly recap the append method that allows you add an arbitrary element to a given list.

How can you add an elements to a given list? Use the append() method in Python.

Definition and Usage

The list.append(x) method—as the name suggests—appends element x to the end of the list.

Here’s a short example:

>>> l = []
>>> l.append(42)
>>> l
[42]
>>> l.append(21)
>>> l
[42, 21]

In the first line of the example, you create the list l. You then append the integer element 42 to the end of the list. The result is the list with one element [42]. Finally, you append the integer element 21 to the end of that list which results in the list with two elements [42, 21].

Syntax

You can call this method on each list object in Python. Here’s the syntax:

list.append(element)

Arguments

Argument Description
element The object you want to append to the list.

Related articles:

Python One Line List Append

Problem: How can you create a list and append an element to a list using only one line of Python code?

You may find this challenging because you must accomplish two things in one line: (1) creating the list and (2) appending an element to it.

Solution: We use the standard technique to one-linerize each “flat” multi-line code snippet: with the semicolon as a separator between the expressions.

a = [1, 2, 3]; a.append(42); print(a)

This way, we accomplish three things in a single line of Python code:

  • Creating the list [1, 2, 3] and assigning it to the variable a.
  • Appending the element 42 to the list referred to by a.
  • Printing the list to the shell.

Related Article: Python One Line to Multiple Lines

Python One Line For Append

Problem: How can we append multiple elements to a list in a for loop but using only a single line of Python code?

Example: Say, you want to filter a list of words against another list and store the resulting words in a new list using the append() method in a for loop.

# FINXTER TUTORIAL:
# How to filter a list of words? words = ['hi', 'hello', 'Python', 'a', 'the']
stop_words = {'a', 'the'}
filtered_words = [] for word in words: if word not in stop_words: filtered_words.append(word) print(filtered_words)
# ['hi', 'hello', 'Python']

You first create a list of words to be filtered and stored in an initially empty list filtered_words. Second, you create a set of stop words against you want to check the words in the list. Note that it’s far more efficient to use the set data structure for this because checking membership in sets is much faster than checking membership in lists. See this tutorial for a full guide on Python sets.

You now iterate over all elements in the list words and add them to the filtered_words list if they are not in the set stop_words.

Solution: You can one-linerize this filtering process using the following code:

filtered_words = [word for word in words if word not in stop_words]

The solution uses list comprehension to, essentially, create a single-line for loop.

Here’s the complete code that solves the problem using the one-liner filtering method:

# FINXTER TUTORIAL:
# How to filter a list of words? words = ['hi', 'hello', 'Python', 'a', 'the']
stop_words = {'a', 'the'}
filtered_words = [word for word in words if word not in stop_words] print(filtered_words)
# ['hi', 'hello', 'Python']

Here’s a short tutorial on filtering in case you need more explanations:

Related Article: How to Filter a List in Python?

Python One Line If Append

In the previous example, you’ve already seen how to use the if statement in the list comprehension statement to append more elements to a list if they full-fill a given condition.

How can you filter a list in Python using an arbitrary condition? The most Pythonic and most performant way is to use list comprehension [x for x in list if condition] to filter all elements from a list.

Try It Yourself:

The most Pythonic way of filtering a list—in my opinion—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 condition.

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 a list and it’s also the most Pythonic one. If you look for alternatives though, keep reading because I’ll explain to you each and every nuance of filtering lists in Python in this comprehensive guide.

Python Append One Line to File

Problem: Given a string and a filename. How to write the string into the file with filename using only a single line of Python code?

Example: You have filename 'hello.txt' and you want to write string 'hello world!' into the file.

hi = 'hello world!'
file = 'hello.txt' # Write hi in file '''
# File: 'hello.txt':
hello world! '''

How to achieve this? In this tutorial, you’ll learn four ways of doing it in a single line of code!

Here’s a quick overview in our interactive Python shell:

Exercise: Run the code and check the file 'hello.txt'. How many 'hello worlds!' are there in the file? Change the code so that only one 'hello world!' is in the file!

The most straightforward way is to use the with statement in a single line (without line break).

hi = 'hello world!'
file = 'hello.txt' # Method 1: 'with' statement
with open(file, 'a') as f: f.write(hi) '''
# File: 'hello.txt':
hello world! '''

You use the following steps:

  • The with environment makes sure that there are no side-effects such as open files.
  • The open(file, 'a') statement opens the file with filename file and appends the text you write to the contents of the file. You can also use open(file, 'w') to overwrite the existing file content.
  • The new file returned by the open() statement is named f.
  • In the with body, you use the statement f.write(string) to write string into the file f. In our example, the string is 'hello world!'.

Of course, a prettier way to write this in two lines would be to use proper indentation:

with open(file, 'a') as f: f.write(hi)

This is the most well-known way to write a string into a file. The big advantage is that you don’t have to close the file—the with environment does it for you! That’s why many coders consider this to be the most Pythonic way.

You can find more ways on my detailed blog article.

Related Article: Python One-Liner: Write String to File

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 Array

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!

Related Article: Print a Python List Beautifully [Click & Run Code]

Python If Else One Line Array

The most basic ternary operator x 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:

Related Article: If-Then-Else in One Line Python [Video + Interactive Code Shell]

Python One Line For Loop Array

How to Write a For Loop in a Single Line of Python Code?

There are two ways of writing a one-liner for loop:

  • 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:

Related article: Python One Line For Loop [A Simple Tutorial]

Python Iterate Array One Line

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:

[print(i**2) for i in lst] '''
1
4
9 '''

Related article: List Comprehension Full Introduction

Python Fill Array One Line

Do you want to fill or initialize an array with n values using only a single line of Python code?

To fill an array with an integer value, use the list multiplication feature:

array = [0] * 10
print(array)
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

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:

Exercise: Use list comprehension to create a list of square numbers.

Let’s dive into some more specific ways to create various forms of lists in Python.

Related Article: How to Create a Python List?

Python Filter Array One Line

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.

Related Article: How to Filter a List in Python?

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

How To Display The Latest Python News On Your Webpage?

To display the latest Python news on your website, you can use the embed feature in your WordPress editor….

Problem: How To Display The Latest Python News On Your Webpage?

Example:

Given a dictionary:

d = {'a': 42, 'b': 21}

We want to obtain the sum of all values in the dictionary:

42+21=63

Method 1: Extract Values and Use sum() Functions

fjfjk

print(42)

fdskalfjlk

Method 2: Extract Values and Use sum() Functions

fjfjk

print(42)

fdskalfjlk

Method 3: Extract Values and Use sum() Functions

fjfjk

print(42)

fdskalfjlk

Method 4: Extract Values and Use sum() Functions

fjfjk

print(42)

fdskalfjlk

Conclusion / Summary

alksjdflkasj

Posted on Leave a comment

Pretty Print JSON [Python One-Liner]

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:

echo '{"Alice": "24", "Bob": "28"}' | python -m json.tool

The output is the prettier:

{ "Alice": "24", "Bob": "28"
}

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:

jq <<< '{ "foo": "lorem", "bar": "ipsum" }'
{ "bar": "ipsum", "foo": "lorem"
}

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

How to Print Without Newline in Python—A Simple Illustrated Guide

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

Print Without Newline Python (End Argument)

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.

Related Article: Python Print Function [And Its SECRET Separator & End Arguments]

Consider the following example:

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 range iterable 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!

Related Article: Print a Python List Beautifully [Click & Run Code]

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 And/Or

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-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 Small Integer Caching: == versus is

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?

The result is a=257.

The reason is an implementation detail of the CPython implementation called “Small Integer Caching” — the internal cache of integers in Python.

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

Python Docs

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.

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

[Collection] URL Decoding Methods

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])"'

Source

Here’s an example usage:

$ urldecode 'q+werty%3D%2F%3B'
q werty=/; $ urlencode 'q werty=/;'
q+werty%3D%2F%3B

Python 3

$ alias urldecode='python3 -c "import sys, urllib.parse as ul; \ print(ul.unquote_plus(sys.argv[1]))"' $ alias urlencode='python3 -c "import sys, urllib.parse as ul; \ print (ul.quote_plus(sys.argv[1]))"'

Here’s an example usage:

$ urldecode 'q+werty%3D%2F%3B'
q werty=/; $ urlencode 'q werty=/;'
q+werty%3D%2F%3B

Source

sed

$ sed 's@+@ @g;s@%@\\x@g' file | xargs -0 printf "%b"

Source

sed with echo -e

$ sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' file | xargs echo -e

Source

sed with alias

For convenience, you may want to use an alias:

$ alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"'

If you want to decode, you can now simply use:

$ echo "http%3A%2F%2Fwww" | urldecode
http://www

Source

Bash

input="http%3A%2F%2Fwww"
decoded=$(printf '%b' "${input//%/\\x}")

Source

To handle pluses (+) correctly, replace them with spaces using sed:

decoded=$(input=${input//+/ }; printf "${input//%/\\x}")

Bash + urlencode() + urldecode() Functions

urlencode() { # urlencode <string> 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 '%%%02X' "'$c" ;; esac done
} urldecode() { # urldecode <string> local url_encoded="${1//+/ }" printf '%b' "${url_encoded//%/\\x}"
}

Sources:

bash + xxd

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
}

Sources:

PHP

$ echo oil+and+gas | php -r 'echo urldecode(fgets(STDIN));' // Or: php://stdin
oil and gas

Source

PHP Library

php -r 'echo urldecode("oil+and+gas");'

Source

Perl

decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")

Source

Perl to Process File

perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file

Source

awk

awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..

Sources:

Python 2 urllib.unquote

The urllib.unquote is a special function in Python’s built-in standard library urllib that does what you need:

decoded_url=$(python2 -c 'import sys, urllib; print urllib.unquote(sys.argv[1])' "$encoded_url")

You can also use it to modify a file:

python2 -c 'import sys, urllib; print urllib.unquote(sys.stdin.read())' <file >file.new &&
mv -f file.new file

Source: https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding

Python 3 urllib.parse.unquote

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.

decoded_url=$(python3 -c 'import sys, urllib.parse; print(urllib.parse.unquote(sys.argv[1]))' "$encoded_url")

Again, you can use the function to process a file as follows:

python3 -c 'import sys, urllib; print(urllib.parse.unquote(sys.stdin.read()))' <file >file.new &&
mv -f file.new file

Source: https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding

Perl URI::Escape

The URI::Escape solves the problem of URL decoding for Perl users.

decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")

You can use the function to process a file as follows:

perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file

Source: https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding

Perl One-Liner Without Installing Modules

$ perl -pe 's/\%(\w\w)/chr hex $1/ge'

Here’s a usage example:

$ echo '%21%22' | perl -pe 's/\%(\w\w)/chr hex $1/ge'
!"

Source: https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding

Bash Regex

$ function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }

Now, you can use the function as a command like this:

$ urldecode https%3A%2F%2Fgoogle.com%2Fsearch%3Fq%3Durldecode%2Bbash
https://google.com/search?q=urldecode+bash

If you need to assign some variables, use this strategy:

$ x="http%3A%2F%2Fstackoverflow.com%2Fsearch%3Fq%3Durldecode%2Bbash"
$ y=$(urldecode "$x")
$ echo "$y"
http://stackoverflow.com/search?q=urldecode+bash

Source: https://stackoverflow.com/questions/6250698/how-to-decode-url-encoded-string-in-shell

GNU Awk

#!/usr/bin/awk -fn
@include "ord"
BEGIN { RS = "%.."
}
{ printf "%s", $0 if (RT != "") { printf "%s", chr("0x" substr(RT, 2)) }
}

Source: https://stackoverflow.com/questions/6250698/how-to-decode-url-encoded-string-in-shell

References

Posted on Leave a comment

Python One Line to Multiple Lines

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.

After publishing an article on how to condense multiple lines into a single line of Python code, many Finxters asked: How to break a long line to multiple lines in Python? This article shows you the best way of breaking a long-winded one-liner into multiple lines to improve readability and unclutter your code.

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

Break a Long Line to Multiple Lines Python

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:

a = 1 + 2 + 3 + 4 - 5 * 2 b = 1 \ + 2 + \ 3 + 4\ - 5 *\ 2 print(a)
# 0 print(b)
# 0

Seeing the messy code in the previous example may cause you to ask:

Should a Line Break Before or After a Binary Operator?

I’ll give the following answer based on the PEP 8 Style Guide (highlights by me):

For decades the recommended style was to break after binary operators. But this can hurt readability in two ways:

  • the operators tend to get scattered across different columns on the screen, and
  • each operator is moved away from its operand and onto the previous line.

Here, the eye has to do extra work to tell which items are added and which are subtracted:

# Wrong:
results = (variable1 + variable2 + (variable3 - variable4) - variable5 - variable6)

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

Thus, the following code is proposed:

# Correct:
results = (variable1 + variable2 + (variable3 - variable4) - variable5 - variable6)

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.

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!