New Features in Python 3.9

Photo by Morioh

Just like other Python geeks, I’m always excited to explore and use the latest features of Python. This article will provide some basic overview of different features which are introduced in Python 3.9.

Based on the information that I got through Python 3.9 release notes, I wanted share this information with you to give a glimpse of latest features in Python 3.9

In this article we will be covering following two features which are introduced in Python 3.9:

  • Dictionary Union
  • String Methods (Remove Suffix / Prefix)

Dictionary Union:

Two new ways introduced in Python 3.9 to merge dictionaries. One is to merge using | operator and the other one is to update the existing dictionary with the content of another dictionary using |= operator.

Let’s see the following example to understand it better by using | operator:

>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> e | d
{'cheese': 3, 'aardvark': 'Ethel', 'spam': 1, 'eggs': 2}

Dict union will return a new dict consisting of the left operand merged with the right operand, each of which must be a dict (or an instance of a dict subclass). If a key appears in both operands, the last-seen value (i.e. that from the right-hand operand) win

The second way to merge dictionaries is called Augmented assignment using |= operator. As you can see in the following example:

>>> d |= e
>>> d
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}

d |= e will modify the d itself instead of creating a new dictionary which happens in union operator | .

Well these both dictionary union operator seems an awesome new feature but they have limitations as well which are:

  • Dict union is not commutative d|e != e|d
  • Repeated dict union is inefficient. e | f | g | h will create and destroys three mappings
  • Dict union is lossy (values may disappear)

String Methods (Remove Suffix / Prefix):

Two new methods are introduced in built-in str class, which are removeprefix and removesuffix .These methods would remove a prefix or suffix (respectively) from a string, if present, and would be added to Unicode str objects, binary bytes and bytearray objects.

By using these methods, your code can be:

  • Less Fragile
  • More performant
  • More descriptive

Let’s dive into example to check how this can improve our code:

## Current Approachif  name.startswith("test_"):

and the improved one:


The purpose of this article is to get you familiar with some new features of Python 3.9, so you can keep yourself up-to-date with the latest features of Python and use them in your code to make your code more efficient and up-to-date.

If you have any questions, please feel free to leave a comment below. Follow me on medium




A Python developer with 5 years of broad expertise in Django, Flask, JavaScript, Back-end Development, and web scraping/automation areas.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

5-ish ways to get your data into Cloud Storage

The increasing complexity of web technologies could open a can of security worms in web…

How to Automate and Schedule Tasks with Crontab on Ubuntu 16.04

Getting Started with bareASGI

Java 8 | Pattern Practice 3 — Diamond

Blue Team: System Live Analysis [Part 2]- Windows: Rules and Tools

Carbon Black Response: Process GeoIP

On clouds and business

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rizwan Butt

Rizwan Butt

A Python developer with 5 years of broad expertise in Django, Flask, JavaScript, Back-end Development, and web scraping/automation areas.

More from Medium

Quick overview of random module in Python

Python Virtual Environments

A “reference” for Python Variables

Mergesort & Quicksort: a simple Python implementation.

Sorting algorithms