What to expect when you’re least expecting!

Photo by Yan from Pexels

Many posts out there catalog the “gotchas” of Python and/or its most popular packages. This blog is yet another entry in that series, but with one difference: I’ve actually, genuinely made all these mistakes myself (some of them with embarrassing frequency). However, just knowing the definition and type of each classic Python object should help you avoid most (if not all) of these mistakes in your work!

Without any further ado, let’s begin.

1. Truthy Or Falsy: NumPy.nan and Pandas.nan

You probably know that for checking if an object’s value is True or False, you can do something like the following:


Finding bottlenecks and optimizing performance using cProfile

Photo by Anthony Maggio from Pexels

If you’ve ever written a line of code (or even tens of thousands of lines), you’ve surely wondered “Why does my code take so long to run?” Answering that question isn’t always simple, but it can be easier if you search for answers the right way.

Perhaps you trust your knowledge of the problem at hand, using your subject matter expertise to check certain snippets first. Maybe you time a few different modules/classes/functions to see where most of the execution time is spent. Better yet, you can profile your code to get more information about the relative time spent in…


Plot lat/long data on the map with lines connecting the locations

A while ago, I needed to plot the lines connecting the latitude (lat) and longitude (long) coordinates of some locations on the map. What I needed to draw is known as the “hub-and-spoke” plot. Knowing that Plotly already has some samples such as the one here, I was able to do it pretty easily. But real-life data won’t always lend itself perfectly to Plotly’s examples, and there may be other requirements that out-of-the-box examples don’t meet.

In this blog, I’ll show you how to build one of these graphs on your own. In particular, we’ll assume that:

  • The data is…


A collection of small useful functions in Python

Hand holding a fork on top of a piece of chocolate cake
Hand holding a fork on top of a piece of chocolate cake
Photo by Jordane Mathieu on Unsplash

Disclaimer: This is a collection of some useful bite-sized functions I’ve found around the web, mainly on Stack Overflow or Python’s documentation page. Some may look trivial, but one way or another, I’ve used them all in my projects and I think they are worth sharing. You can find all of them (with some additional comments) in this notebook which I try to keep up to date. If you are interested, you can check my first blog on bite-sized functions here!

Unless necessary, I intend not to over-explain the functions. So, let’s begin!

Return the First N Items of an…


A collection of small useful functions in Python

Photo by Jordane Mathieu on Unsplash

Disclaimer: This is a collection of small useful functions I’ve found around the web, mainly on Stack Overflow or Python’s documentation page. Some may look, but one way or another, I have used them all in my projects and I think they are worth sharing. You can find all of them, with some additional comments, in this notebook which I try to keep up to date.

Unless necessary, I intend not to over-explain the functions. So, let’s begin!

Create a Dictionary From Two Lists:


A simple structure for optimization modeling in Python using commercial and open-source solvers

Photo by Chris Ried on Unsplash

For those who are in a hurry!

Go to this GitHub repository, take a look at the short description in the README file, read the comments in the scripts, and play around with them. I hope you find it useful. And of course, your comments are really appreciated!

You may also check out my colleague’s blog where he covered high-level optimization modeling in Python with Gurobi, CPLEX, and PuLP.

For those who can stick around!

In a previous blog, I introduced the simple “input →process →output” (IPO) framework and discussed how it can improve the structure of our code. So if you need some backstory, check out that blog first. In this post…


How to follow the Input-Process-Output flow

Photo by JohnsonMartin on Pixabay

Unless you’re a professional programmer, if you’ve ever sat down to write a piece of code, you’ve probably begun by pausing and wondering, “Where should I start?” On top of that, other people’s code can seem completely inscrutable if the documentation or motivation isn’t clear. So when it comes to code, it’s not uncommon to feel lost.

The challenge of not knowing how or where to start doesn’t result from a lack of understanding of the problem, or an inability to frame a solution. In the case of reading someone else’s code, it’s not just a lack of good documentation…


Photo by Franki Chamaki on Unsplash

If you work with data, you know how crucial it is to validate it before modeling. As critical as it is, data validation is often overlooked during a project as it’s often perceived as less interesting in comparison to other aspects of the modeling process.

As I shared in a previous post, I believe that checklists are useful tools in overcoming failures and reducing errors, especially in routine tasks. This time, I want to share one of the checklists I often use as soon as I receive a new dataset. Keep in mind: I don’t intend to offer an overly…


In an earlier post, we discussed simple load consolidation and the fact that it’s not very simple after all. In this blog, we’ll examine another transportation problem many companies face on a daily basis, and we’ll explore alternative solutions. (This scenario has been borrowed from a client, but names and values have been changed.)

Our client, Company XYZ, has a central depot in Indiana and is shipping products from this depot to three different destinations: Kentucky, Georgia, and Wisconsin. Figure 1 illustrates its network. We have two modes of transport for shipping the products: truckload (TL) and less-than-truckload (LTL). …


Source: pixabay

Recently, I have read a book called “Checklist Manifesto”. As one might guess, this book was all about checklists! I don’t even know why I picked the book up in the first place: maybe I was very curious by the idea that someone, a surgeon, can write a 200-page book just about checklists! Or maybe because I’m a fan of lists and checklists. In fact, if I create a list of things I like, “lists” is definitely among the top items on that list!

Anyway, the whole premise of the book revolved around the idea that, though they seem to…

Ehsan Khodabandeh

Operations Research Scientist. I write about optimization, logistics, and occasionally Python!

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