Posted on Leave a comment

Python How to Join a List of Dictionaries into a Single One?

Problem: Say, you’ve got a list of dictionaries:

[{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}]

Notice how the first and the last dictionaries carry the same key 'a'.

How do you merge all those dictionaries into a single dictionary to obtain the following one?

{'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

Notice how the value of the duplicate key 'a' is the value of the last and not the first dict in the list of dicts.

To merge multiple dictionaries, the most Pythonic way is to use dictionary comprehension {k:v for x in l for k,v in x.items()} to first iterate over all dictionaries in the list l and then iterate over all (key, value) pairs in each dictionary.

Let’s explore all the available options in the remaining article:

Exercise: Run the code—which method generates a different output than all the other methods?

Method 1: Dictionary Comprehension With Nested Context

You can use dictionary comprehension {k:v for x in l for k,v in x.items()} to first iterate over all dictionaries in the list l and then iterate over all (key, value) pairs in each dictionary.

  • Create a new dictionary using the {...} notation.
  • Go over all dictionaries in the list of dictionaries l by using the outer loop for x in l.
  • Go over all (key, value) pairs in the current dictionary x by using the x.items() method that returns an iterable of (key, value) pairs.
  • Fill the new dictionary with (key, value) pairs k:v using the general dictionary comprehension syntax {k:v for ...}.
l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {k:v for x in l for k,v in x.items()}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

This is the most Pythonic way to merge multiple dictionaries into a single one and it works for an arbitrary number of dictionaries.

Method 2: Simple Nested Loop

Use a simple nested loop to add each (key, value) pair separately to a newly created dictionary:

  • Create a new, empty dictionary.
  • Go over each dictionary in the list of dictionaries.
  • Go over each (key, value) pair in the current dictionary.
  • Add the (key, value) pair to the new dictionary—possibly overwriting “older” keys with the current one.
l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {}
for dictionary in l: for k, v in dictionary.items(): d[k] = v print(d)
{'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

You can visualize the execution flow of this code here:

This is the easiest to read for many beginner coders—but it’s much less concise and less Pythonic.

Method 3: Use the update() Method

The dict.update(x) method updates the dictionary on which it is called with a bunch of new (key, value) pairs given in the dictionary argument x. The method to merge multiple dictionaries is simple:

  • Create a new, empty dictionary.
  • Go over each dictionary in the list of dictionaries.
  • Update the new dictionary with the values in the current dictionary.
l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {}
for dictionary in l: d.update(dictionary) print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

This is a very readable and efficient way and it’s shorter than method 2.

Method 4: Dictionary Unpacking

When applied to a dictionary d, the double asterisk operator **d unpacks all elements in d into the outer dictionary. You can only use this “dictionary unpacking” method within an environment (in our case a dictionary) that’s capable of handling the (key, value) pairs.

Side note: Sometimes it’s also used for keyword arguments.

l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {**l[0], **l[1], **l[2], **l[3], **l[4]}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

This is a concise, efficient, and Pythonic way to merge multiple dictionaries. However, it’s not optimal because you must manually type each unpacking operation. If the dictionary has 100 elements, this wouldn’t be feasible.

Note: you cannot use dictionary unpacking in dictionary comprehension to alleviate this problem—Python will throw a SyntaxError!

Method 5: Use ChainMap With Unpacking

If you’re not happy with either of those methods, you can also use the ChainMap data structure from the collections library.

l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] from collections import ChainMap
d = dict(ChainMap(*l))
print(d)
# {'e': 4, 'a': 0, 'd': 3, 'c': 2, 'b': 1}

However, this does not exactly meet our specifications: the fifth dictionary in our collection does not overwrite the (key, value) pairs of the first dictionary. Other than that, it’s a fast way to merge multiple dictionaries and I wanted to include it here for comprehensibility.

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 Join List of Bytes (and What’s a Python Byte Anyway?)

When teaching the Finxter Python students, I’m often shocked how even intermediate-level coders do not understand the very basic foundations of computer science. In CS, there’s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called bits).

This tutorial aims to clarify some misconceptions and questions regarding Bytes in Python—a term that has a quite different meaning to bytes in general. In particular, you’ll learn the definition of the Byte object and how to correctly join a sequence of Bytes. Here’s the short answer:

A Python Byte is a sequence of bytes. You create a Byte object using the notation b'...' similar to the string notation '...'. To join a list of Bytes, call the Byte.join(list) method. If you try to join a list of Bytes on a string delimiter, Python will throw a TypeError, so make sure to call it on a Byte object b' '.join(...) rather than ' '.join(...).

Before you dive into all of this in a step-by-step manner, let’s play a bit with the interactive code shell to open your knowledge gap:

Exercise: What happens if you join the list of Bytes on a string delimiter? Try it in the shell!

What’s a Byte in Python Anyway?

You think you know what a byte is, right? It’s a sequence of 8 bits.

Well, that’s what a byte is outside the Python world. In Python, it’s not the whole story.

Python comes with a built-in Byte data type. According to the official documentation, the Byte object is an “immutable sequence of bytes“. If you talk about a byte outside Python, you mean 8 bits. If you talk about a byte inside Python, you mean a sequence of one or more bytes.

Let’s keep that on a hold for a moment and look how you create a simple byte object:

>>> a = b'X'
>>> a
b'X'
>>> type(a)
<class 'bytes'>

You create the Byte object just like you create a string in Python using the single, double, or triple quotes around a text that’s to be encoded in a “sequence of bytes”. In this case, the sequence of bytes consists only of the bytes needed to encode a single character 'X'. The ASCII code of character 'X' is 88, or in binary format 0b1011000. You can see that you only need one byte to encode the character.

But can you also create more complicated “Python Byte” objects that consist of more “real bytes”? Sure, you can:

>>> a = b'hello'
>>> a
b'hello'
>>> type(a)
<class 'bytes'>

Again, you create the Byte object for 'hello'. You cannot encode the whole string 'hello' in eight bits (=byte), so the Python Byte object a now consists of a “sequence of bytes”.

Takeaway: A (Python) Byte is a sequence of bytes and you can use it just like you use strings by using the syntax b'...' instead of '...'. As string objects, Bytes are immutable—so you cannot modify them after creation.

Concatenate Byte Strings in Python

You can concatenate two byte objects x and y in Python by using the (overloaded) add operation x+y:

>>> x = b'hello '
>>> y = b'world!'
>>> x + y
b'hello world!'

This works for Python 2 and Python 3—and it will probably also work in Python 4! (Why? Because the semantics of the __add__ operation won’t change just by setting up a new programming language or modifying an old one.)

How to Join a List of Bytes?

Python Bytes are similar than Python strings (at least for you, the person who used the Byte data structure in their program).

So, joining a list of Bytes is similar than joining a list of strings: use the join method! The Byte object comes with a method Byte.join(iterable) that concatenates all Byte objects in the iterable.

Keep in mind that a Byte object is a sequence of bytes by itself (and not a sequence of bits as you may have expected). And, yes, I’ll repeat this until it sticks.

Syntax: Byte.join(iterable)

Argument Description
iterable A collection of Byte objects.

Examples: Let’s see a bunch of examples on how you can join a collection of Byte objects!

lst = [b'Python', b'is', b'beautiful'] # Example 1
print(b' '.join(lst))
b'Python is beautiful' # Example 2
print(b'-'.join(lst))
b'Python-is-beautiful' # Example 3
print(b'\n'.join(lst))
b'Python\nis\nbeautiful'

You get the point: you call the join() method on the Byte object and pass an iterable of Byte objects to be concatenated. Notice how all involved data types are Byte objects!

How to Fix “TypeError: sequence item 0: expected str instance, bytes found”?

A common mistake of many Python coders is to call the wrong join() method! Say, you’ve got an iterable of Bytes to be joined. If you call the string.join(...) method instead of the Byte.join(...) method, Python will throw a TypeError with the following error message:

lst = [b'Python', b'is', b'beautiful']
print(' '.join(lst))
# TypeError: sequence item 0: expected str instance, bytes found

The error message doesn’t directly tell you how to fix this issue. The reason is that you call the join() method on the string object. And the string join() method expects you to pass an iterable of string objects to be concatenated. But you pass an iterable of Byte objects!

To fix it, simply call the join(...) method on the Byte object b' '.join(...) instead of ' '.join(...).

lst = [b'Python', b'is', b'beautiful']
print(b' '.join(lst))
b'Python is beautiful'

This way, you can easily concatenate multiple Byte objects by using the Byte.join() method.

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

How to Intersect Multiple Sets in Python?

To intersect multiple sets, stored in a list l, use the Python one-liner l.pop().intersection(*l) that takes the first set from the list, calls the intersection() method on it, and passes the remaining sets as arguments by unpacking them from the list.

A set is a unique collection of unordered elements. The intersection operation creates a new set that consists of the elements that exist in all sets.

So, let’s dive into the formal problem formulation, shall we?

Problem: Given a list or a collection of sets. How to join those sets using the intersection operation?

Example: You’ve got a list of sets [{1, 2, 3}, {1, 4}, {2, 3, 5}] and you want to calculate the intersection {1}.

Solution: To intersect a list of sets, use the following strategy:

  • Get the first element from the list as a starting point. This assumes the list has at least one element.
  • Call the intersection() method on the first set object.
  • Pass all sets as arguments into the intersection() method by unpacking the list with the asterisk operator *list.
  • The result of the intersection() method is a new set containing all elements that are in all of the sets.

Code: Here’s the one-liner code that intersects a collection of sets.

# Create the list of sets
lst = [{1, 2, 3}, {1, 4}, {1, 2, 3}] # One-Liner to intersect a list of sets
print(lst[0].intersection(*lst))

The output of this code is the intersection of the three sets {1, 2, 3}, {1, 4}, and {2, 3, 5}. Only one element appears in all three sets:

{1}

If you love Python one-liners, check out my new book “Python One-Liners” (Amazon Link) that teaches you a thorough understanding of all single lines of Python code.

Try it yourself: Here’s the code in an interactive code shell that runs it in your browser:

Exercise: Change the code to calculate the union of the sets in the list!

Related video: A similar problem is to perform the union operation on a list of sets. In the following video, you can watch me explain how to union multiple sets in Python:

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

The Most Pythonic Way to Convert a List of Tuples to a String

Convert List of Tuples to String

The most Pythonic way to convert a list of tuples to a string is to use the built-in method str(...). If you want to customize the delimiter string, the most Pythonic way is to concatenate the join() method and the map() function '\n'.join(map(str, lst)) to convert all tuples to strings and gluing those together with the new-line delimiter '\n'.

Exercise: Run the interactive code snippet. Which method do you like most?

Method 1: Default String Conversion

Say, you’ve got a list of tuples, and you want to convert it to a string (e.g., see this SO post). The easiest way to accomplish this is to use the default string conversion method str(...).

>>> lst = [(1,1), (2,1), (4,2)]
>>> str(lst) '[(1, 1), (2, 1), (4, 2)]'

The result is a nicely formatted string representation of your list of tuples.

Method 2: Join() and Map()

However, if you want to customize the delimiter string, you can use the join() method in combination with the map() function:

lst = [(1,1), (2,1), (4,2)] print('--'.join(map(str, lst)))
# (1, 1)--(2, 1)--(4, 2)

The map() function transforms each tuple into a string value, and the join() method transforms the collection of strings to a single string—using the given delimiter '--'. If you forget to transform each tuple into a string with the map() function, you’ll get a TypeError because the join() method expects a collection of strings.

Method 3: Flatten List of Tuples

If you want to flatten the list and integrate all tuple elements into a single large collection of elements, you can use a simple list comprehension statement [str(x) for t in lst for x in t].

lst = [(1,1), (2,1), (4,2)] print('\n'.join([str(x) for t in lst for x in t])) '''
1
1
2
1
4
2 '''

If you want to redefine how to print each tuple—for example, separating all tuple values by a single whitespace character—use the following method based on a combination of the join() method and the map() function with a custom lambda function lambda x: str(x[0]) + ' ' + str(x[1]) to be applied to each list element.

lst = [(1,1), (2,1), (4,2)] print('\n'.join(map(lambda x: str(x[0]) + ' ' + str(x[1]), lst))) '''
1 1
2 1
4 2 '''

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

{AND, OR, NOT} How to Apply Logical Operators to All List Elements in Python?

Problem: Given a list of Boolean elements. What’s the best way to join all elements using the logical “OR” and logical “AND” operations?

Example: Convert the list [True, True, False] using

  • The logical “AND” operation to True and True and False = False,
  • The logical “OR” operation to True or True or False = True, and
  • The logical “NOT” operation to [not True, not True, not False] = [False, False, True].

Solution:

  • To perform logical “AND”, use the built-in Python function all(),
  • To perform logical “OR”, use the built-in Python function any(), and
  • To perform logical “NOT”, use a list comprehension statement [not x for x in list].

Here’s the solution for our three examples:

lst = [True, True, False] # Logical "AND"
print(all(lst))
# False # Logical "OR"
print(any(lst))
# True # Logical "NOT"
print([not x for x in lst])
# [False, False, True]

This way, you can combine an arbitrary iterable of Booleans into a single Boolean value.

Puzzle: Guess the output of this interactive code snippet—and run it to check if you were correct!

The challenge in the puzzle is to know that Python comes with implicit Boolean type conversion: every object has an associated Boolean value. Per convention, all objects are True except “empty” or “zero” objects such as [], '', 0, and 0.0. Thus, the result of the function call all([True, True, 0]) is False.

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 Join List with Underscore [The Most Pythonic Way]

The '_'.join(list) method on the underscore string '_' glues together all strings in the list using the underscore string as a delimiter—and returns a new string. For example, the expression '_'.join(['a', 'b', 'c']) returns the new string 'a_b_c'.

Definition and Usage

The string.join(iterable) method joins the string elements in the iterable to a new string by using the string on which it is called as a delimiter.

Here’s a short example:

friends = ['Alice', 'Bob', 'Carl'] # Empty delimiter string ''
print(''.join(friends))
# AliceBobCarl # Delimiter string '_'
print('_'.join(friends))
# Alice_Bob_Carl

The string elements in the list friends are concatenated using the delimiter string '' in the first example and the underscore character '_' in the second example.

Related articles:

Syntax

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

string.join(iterable)

Argument Description
iterable The elements to be concatenated.

Code Puzzle

To practice what you’ve learned so far, feel free to solve the following interactive code puzzle:

Exercise: Guess the output and check if you were right by running the interactive code shell.

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

How to Join a List of Lists? You Don’t. You Flatten It!

Short answer: To flatten your list of list lst, use the nested list comprehension [x for l in lst for x in l] to iterate over all nested lists (for l in lst) and over all their elements (for x in l). Each element x, you just place in the outer list, unchanged.

When browsing StackOverflow, I stumble upon this question from time to time: “How to Join a List of Lists?“. My first thought is that the asking person is referring to the join() function that converts an iterable (such as a list) to a string by concatenating its elements.

But nothing can be further from the truth! The question is usually about “flattening” the list: to transform a nested list of lists such as [[1, 2, 3], [4, 5, 6]] to a flat list [1, 2, 3, 4, 5, 6]. So, the real question is:

How to Flatten a List of Lists in Python?

Problem: Given a list of lists. How to flatten the list of lists by getting rid of the inner lists—and keeping their elements?

Example: You want to transform a given list into a flat list like here:

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ... print(lst)
# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]
Flatten a List of Lists with List Comprehension

Solution: Use a nested list comprehension statement [x for l in lst for x in l] to flatten the list.

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ...
lst = [x for l in lst for x in l] print(lst)
# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]

Explanation: In the nested list comprehension statement [x for l in lst for x in l], you first iterate over all lists in the list of lists (for l in lst). Then, you iterate over all elements in the current list (for x in l). This element, you just place in the outer list, unchanged, by using it in the “expression” part of the list comprehension statement [x for l in lst for x in l].

Try It Yourself: You can execute this code snippet yourself in our interactive Python shell. Just click “Run” and test the output of this code.

Exercise: How to flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!

Video: List Comprehension Python List of Lists

Watch the video to learn three ways how to apply list comprehension to a list of lists:

  • to flatten a list of lists,
  • to create a list of lists, and
  • to iterate over a list of lists.

Well, if you really want to learn how to join() a list in Python, visit this detailed tutorial on the Finxter blog.

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

Use FastAPI to build web services in Python

FastAPI is a modern Python web framework that leverage the latest Python improvement in asyncio. In this article you will see how to set up a container based development environment and implement a small web service with FastAPI.

Getting Started

The development environment can be set up using the Fedora container image. The following Dockerfile prepares the container image with FastAPI, Uvicorn and aiofiles.

FROM fedora:32
RUN dnf install -y python-pip \ && dnf clean all \ && pip install fastapi uvicorn aiofiles
WORKDIR /srv
CMD ["uvicorn", "main:app", "--reload"]

After saving this Dockerfile in your working directory, build the container image using podman.

$ podman build -t fastapi .
$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/fastapi latest 01e974cabe8b 18 seconds ago 326 MB

Now let’s create a basic FastAPI program and run it using that container image.

from fastapi import FastAPI app = FastAPI() @app.get("/")
async def root(): return {"message": "Hello Fedora Magazine!"}

Save that source code in a main.py file and then run the following command to execute it:

$ podman run --rm -v $PWD:/srv:z -p 8000:8000 --name fastapi -d fastapi
$ curl http://127.0.0.1:8000
{"message":"Hello Fedora Magazine!"

You now have a running web service using FastAPI. Any changes to main.py will be automatically reloaded. For example, try changing the “Hello Fedora Magazine!” message.

To stop the application, run the following command.

$ podman stop fastapi

Building a small web service

To really see the benefits of FastAPI and the performance improvement it brings (see comparison with other Python web frameworks), let’s build an application that manipulates some I/O. You can use the output of the dnf history command as data for that application.

First, save the output of that command in a file.

$ dnf history | tail --lines=+3 > history.txt

The command is using tail to remove the headers of dnf history which are not needed by the application. Each dnf transaction can be represented with the following information:

  • id : number of the transaction (increments every time a new transaction is run)
  • command : the dnf command run during the transaction
  • date: the date and time the transaction happened

Next, modify the main.py file to add that data structure to the application.

from fastapi import FastAPI
from pydantic import BaseModel app = FastAPI() class DnfTransaction(BaseModel): id: int command: str date: str

FastAPI comes with the pydantic library which allow you to easily build data classes and benefit from type annotation to validate your data.

Now, continue building the application by adding a function that will read the data from the history.txt file.

import aiofiles from fastapi import FastAPI
from pydantic import BaseModel app = FastAPI() class DnfTransaction(BaseModel): id: int command: str date: str async def read_history(): transactions = [] async with aiofiles.open("history.txt") as f: async for line in f: transactions.append(DnfTransaction( id=line.split("|")[0].strip(" "), command=line.split("|")[1].strip(" "), date=line.split("|")[2].strip(" "))) return transactions

This function makes use of the aiofiles library which provides an asyncio API to manipulate files in Python. This means that opening and reading the file will not block other requests made to the server.

Finally, change the root function to return the data stored in the transactions list.

@app.get("/")
async def read_root(): return await read_history()

To see the output of the application, run the following command

$ curl http://127.0.0.1:8000 | python -m json.tool
[
{ "id": 103, "command": "update", "date": "2020-05-25 08:35"
},
{ "id": 102, "command": "update", "date": "2020-05-23 15:46"
},
{ "id": 101, "command": "update", "date": "2020-05-22 11:32"
},
....
]

Conclusion

FastAPI is gaining a lot a popularity in the Python web framework ecosystem because it offers a simple way to build web services using asyncio. You can find more information about FastAPI in the documentation.

The code of this article is available in this GitHub repository.


Photo by Jan Kubita on Unsplash.

Posted on Leave a comment

Python Join List [Ultimate Guide]

In this ultimate guide, you’ll learn everything you need to know about joining list elements in Python. To give you a quick overview, let’s have a look at the following problem.

Problem: Given a list of elements. How to join the elements by concatenating all elements in the list?

Example: You want to convert list ['learn ', 'python ', 'fast'] to the string 'learn python fast'.

Quick Solution: to convert a list of strings to a string, do the following.

  • Call the ''.join(list) method on the empty string '' that glues together all strings in the list and returns a new string.
  • The string on which you call the join method is used as a delimiter between the list elements.
  • If you don’t need a delimiter, just use the empty string ''.

Code: Let’s have a look at the code.

lst = ['learn ', 'python ', 'fast']
print(''.join(lst))

The output is:

learn python fast

Try it yourself in our interactive Python shell:

You can also use another delimiter string, for example, the comma:

lst = ['learn' , 'python', 'fast']
print(','.join(lst))
# learn,python,fast

Python Join List Syntax

Python Join List of Lists

Python Join List of Strings With Comma

Problem: Given a list of strings. How to convert the list to a string by concatenating all strings in the list—using a comma as the delimiter between the list elements?

Example: You want to convert list ['learn', 'python', 'fast'] to the string 'learn,python,fast'.

Solution: to convert a list of strings to a string, call the ','.join(list) method on the delimiter string ',' that glues together all strings in the list and returns a new string.

Code: Let’s have a look at the code.

lst = ['learn', 'python', 'fast']
print(','.join(lst))

The output is:

learn,python,fast

Python Join List of Strings With Newline

Problem: Given a list of strings. How to convert the list to a string by concatenating all strings in the list—using a newline character as the delimiter between the list elements?

Example: You want to convert list ['learn', 'python', 'fast'] to the string 'learn\npython\nfast' or as a multiline string:

'''learn
python
fast'''

Solution: to convert a list of strings to a string, call the '\n'.join(list) method on the newline character '\n' that glues together all strings in the list and returns a new string.

Code: Let’s have a look at the code.

lst = ['learn', 'python', 'fast']
print('\n'.join(lst))

The output is:

learn
python
fast

Python Join List of Strings With Space

Problem: Given a list of strings. How to convert the list to a string by concatenating all strings in the list—using a space as the delimiter between the list elements?

Example: You want to convert list ['learn', 'python', 'fast'] to the string 'learn python fast'. (Note the empty spaces between the terms.)

Solution: to convert a list of strings to a string, call the ' '.join(list) method on the string ' ' (space character) that glues together all strings in the list and returns a new string.

Code: Let’s have a look at the code.

lst = ['learn', 'python', 'fast']
print(' '.join(lst))

The output is:

learn python fast

Python Join List With Single and Double Quotes

Problem: Given a list of strings. How to convert the list to a string by concatenating all strings in the list—using a comma character followed by an empty space as the delimiter between the list elements? Additionally, you want to wrap each string in double quotes.

Example: You want to convert list ['learn', 'python', 'fast'] to the string '"learn", "python", "fast"' :

Solution: to convert a list of strings to a string, call the ', '.join('"' + x + '"' for x in lst) method on the delimiter string ', ' that glues together all strings in the list and returns a new string. You use a generator expression to modify each element of the original element so that it is enclosed by the double quote " chararacter.

Code: Let’s have a look at the code.

lst = ['learn', 'python', 'fast']
print(', '.join('"' + x + '"' for x in lst))

The output is:

"learn", "python", "fast"

Python Join List With None

Python Join List With Tabs

Python Join List With Delimiter

Python Join List With Carriage Return

Python Join List with Underscore

Python Join List of Integers

Problem: You want to convert a list into a string but the list contains integer values.

Example: Convert the list [1, 2, 3] to a string '123'.

Solution: Use the join method in combination with a generator expression to convert the list of integers to a single string value:

lst = [1, 2, 3]
print(''.join(str(x) for x in lst))
# 123

The generator expression converts each element in the list to a string. You can then combine the string elements using the join method of the string object.

If you miss the conversion from integer to string, you get the following TypeError:

lst = [1, 2, 3]
print(''.join(lst)) '''
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 2, in <module> print(''.join(lst))
TypeError: sequence item 0: expected str instance, int found '''

Python Join List of Floats

Python Join List of Booleans

Python Join List of Tuples

Python Join List of Sets

Python Join List of Bytes

Python Join List of Dictionaries

Python Join List Except First or Last Element

Python Join List Remove Duplicates

Python Join List Reverse

Python Join List Range

Python Join List By Row

Python Join List of Unicode Strings

Python Join List in Pairs

Python Join List as Path

Python Join List Slice

Python Join Specific List Elements

Python Join List of DataFrames

Python Join List Comprehension

Python Join List Map

Python Join List Columns

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

How to Check If a Python List is Empty?

Believe it or not—how you answer this question in your day-to-day code reveals your true Python skill level to every master coder who reads your code.

How to Check if List is Empty Python

Beginner coders check if a list a is empty using crude statements like len(a)==0 or a==[]. While those solve the problem—they check if a list is empty—they are not what a master coder would do. Instead, the most Pythonic way to check if a list (or any other iterable for that matter) is empty is the expression not a.

You may call it implicit Booleanness (or, more formal, type flexibility): every object in Python can be implicityl converted into a truth value.

Here’s an example in our interactive Python shell—try it yourself!

Exercise: What’s the output of the code if you add one element to the list a?

Truth Value Testing and Type Flexibility

Python implicitly associates any object with a Boolean value. Here are some examples:

  • The integers 1, 2, and 3 are associated to the Boolean True.
  • The integer 0 is associated to the Boolean False.
  • The strings 'hello', '42', and '0' are associated to the Boolean True.
  • The empty string '' is associated to the Boolean False.

Roughly speaking, each time a Boolean value is expected, you can throw in a Python object instead. The Python object will then be converted to a Boolean value. This Boolean value will be used to decide whether to enter, say, a while loop or an if statement. This is called “type flexibility” and it’s one of Python’s core design choices.

Per default, all objects are considered True if they are semantically non-empty. Empty objects are usually associated to the Boolean False. More specifically, only if one of the two cases is met, will the result of an object be False: (i) the __len__() function returns 0, or (ii) the __bool__() function returns False. You can redefine those two methods for each object.

From the Python documentation, here are some common objects that are associated to the Boolean False:

  • Defined constants: None and False.
  • Zero of numerical types: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty iterables: '', (), [], {}, set(), range(0)

Here are some examples:

if []: print('1') if (): print('2') if [()]: print('3')
# 3 if 0: print('4') if 0.00: print('5') if 0.001: print('6')
# 6 if set(): print('7') if [set()]: print('8')
# 8

Again, even if the iterable contains only a single element (that may evaluate to False like integer 0), the implicit Boolean conversion will return True because an empty element is an element nonetheless.

PEP8 Recommendation: How to Check if a List is Empty

As some readers argued with me about how to correctly check for an empty list in Python, here‘s the explicit excerpt from the PEP8 standard (Python’s set of rules about how to write readable code):

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

# Correct:
if not seq:
if seq:
# Wrong:
if len(seq):
if not len(seq):

Performance Evaluations

To see which of the three methods is fastest, I repeated each method 100 times using the timeit library on my notebook with Intel Core i7 (TM) CPU of 8th Generation, 8GB RAM—yes, I know—and NVIDIA Graphic Card (not that it mattered).

Here’s the code:

import timeit
import numpy as np setup = 'a = []' method1 = 'if len(a) == 0: pass'
method2 = 'if a == []: pass'
method3 = 'if not a: pass' t1 = timeit.repeat(stmt=method1, setup=setup, repeat=100)
t2 = timeit.repeat(stmt=method2, setup=setup, repeat=100)
t3 = timeit.repeat(stmt=method3, setup=setup, repeat=100) print('Method 1: len(a) == 0')
print('avg: ' + str(np.average(t1)))
print('var: ' + str(np.var(t1)))
print() print('Method 2: a == []')
print('avg: ' + str(np.average(t2)))
print('var: ' + str(np.var(t2)))
print() print('Method 3: not a')
print('avg: ' + str(np.average(t3)))
print('var: ' + str(np.var(t3)))
print()

The third method is the most Pythonic one with type flexibility. We measure the elapsed time of 100 executions of each method. In particular, we’re interested in the average time and the variance of the elapsed time. Both should be minimal.

Our thesis is that the third, most Pythonic method is also the fastest because there’s no need to create a new empty list (like in method 2) or performing nested function calls like in method 1. Method 3 consists only of a single function call: converting the list into a Boolean value with the __bool__ or __len__ methods.

Here’s the result in terms of elapsed average runtime and variance of the runtimes:

Method 1: len(a) == 0
avg: 0.06273576400000003
var: 0.00022597495215430347 Method 2: a == []
avg: 0.034635367999999944
var: 8.290137682917488e-05 Method 3: not a
avg: 0.017685209000000004
var: 6.900910317342067e-05

You can see that the third method is not only 50% faster than method 2 and 75% faster than method 3, it also has very little variance. It’s clearly the best method in terms of runtime performance. Being also the shortest method, you can now see why the method is considered to be most “Pythonic”.

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!