There are at least two common ways to sort lists in Python:
Which one is faster? Let’s find out!
I will start with a list of 1 000 000 randomly shuffled integers. Later on, I will also check if the order matters.
sorted is less than 10% slower (385/352≈1.094). Since we only run one loop, the exact numbers are not very reliable. I have rerun the same tests a couple more times, and the results were slightly different each time.
sort took around 345-355 msec and
sorted took around 379-394 msec (but it was always slower than
Many simple “for loops” in Python can be replaced with list comprehensions. You can often hear that list comprehension is “more Pythonic” (almost as if there was a scale for comparing how Pythonic something is, compared to something else 😉). In this article, I will compare their performance and discuss when a list comprehension is a good idea, and when it’s not.
Let’s use a simple scenario for a loop operation — we have a list of numbers, and we want to remove the odd ones. One important thing to keep in mind is that we can’t remove items from a list as we iterate over it. …
If you worked with Python 2 or an early version of Python 3, you probably remember that, in the past, dictionaries were not ordered. If you wanted to have a dictionary that preserved the insertion order, the go-to solution was to use OrderedDict from the collections module.
In Python 3.6, dictionaries were redesigned to improve their performance (their memory usage was decreased by around 20–25%). This change had an interesting side-effect — dictionaries became ordered (although this order was not officially guaranteed). …
If you have functions that do a lot of mathematical operations, use NumPy or rely heavily on loops, then there is a way to speed them up significantly with one line of code. Ok, two lines if you count the import.
Meet Numba and its @jit decorator. It changes how your code is compiled, often improving its performance. You don’t have to install any special tools (just the
numba pip package), you don't have to tweak any parameters. All you have to do is:
@jitdecorator to a function
Let’s see an example of code before and after applying
Numba's optimization. …
If you want to find the first number that matches some criteria, what do you do? The easiest way is to write a loop that checks numbers one by one and returns when it finds the correct one.
Let’s say we want to get the first number divided by 42 and 43 (that’s 1806). If we don’t have a predefined set of elements (in this case, we want to check all the numbers starting from 1), we might use a “while loop”.
It’s pretty straightforward:
“Ask for forgiveness” and “look before you leap” (sometimes also called “ask for permission”) are two opposite approaches to writing code. If you “look before you leap”, you first check if everything is set correctly, then you perform an action. For example, you want to read text from a file. What could go wrong with that? Well, the file might not be in the location where you expect it to be. So, you first check if the file exists:
Even if the file exists, maybe you don’t have permission to open it? …
I love IPython. I love using it, I love writing about it, I love taking pictures with its core contributors (Hi Paul!). If I ever get invited to the “Talk Python To Me” podcast (not that I have anything interesting to talk about), and Michael Kennedy is going to ask me what my favorite Python package is, you know what I’m going to say? Yep, IPython.
So, when a friend of mine asked me what kind of lightning talk I want to prepare for one of the upcoming micro-conferences, my first thought was: “Let’s try to make something cool with IPython”. …
Previously, I wrote about my favorite Mac apps. But I spend half of my time in the terminal, and I have a handful of CLI tools that makes my life easier. Here are some of them.
Shell — the most important tool that you use every time you open the terminal. I’ve used Bash and Z shell in the past, and currently, I’m using fish. It’s a great shell with plenty of features out of the box, like the autosuggestions, syntax highlighting, or switching between folders with ⌥+→ and ⌥+←.
On the one hand, this makes it perfect for beginners, because you don’t have to set up anything. On the other hand, because it’s using a different syntax than other shells, you usually can’t just paste scripts from the internet. You either have to change the incompatible commands to fish scripts or start a Bash session to run the bash scripts. I understand the idea behind this change (Bash is not the easiest language to use), but it doesn’t benefit me in any way. I write bash/fish scripts too seldom to memorize the syntax, so I always have to relearn it from scratch. And there are fewer resources for fish scripts than for bash scripts. I usually end up reading the documentation, instead of copy-pasting ready-made scripts from StackOverflow. …
Luckily, when you open a file in a new language, VS Code will suggest an extension that can help you. With the Python extension, you can already do a lot — you get syntax highlighting, code completion, and many other features that turn a text editor into a code editor.
But there are many other plugins that I discovered when working with Python. Some add entirely new functionality, and others offer just a small improvement here and there. I’ve decided to write them down. I hope some of you will find them useful! …
I could spend days just tweaking things on my computer. Actually, I do. Whenever I see something annoying, I want to drop everything and try to fix it right away. It can be anything. From a minor: “Hey, I just run the same command twice, I should create an alias!” to installing random tools ( “Hmm, I’m wondering if there is a way to get notifications when a long-running job in a terminal finishes, so I can do other stuff in the meantime?”). I also love to read what tools other people are using. …