Refactor a three-step algorithm into a one-liner

Image for post
Image for post
Photo by Nick Fewings on Unsplash

I love Python’s flexibility. It’s elegant “ah-ha” moments where a clever implementation of native tools yields simplified results that put a smile on my face.

Recently, I had to analyze a data set — a list of dictionaries where one term named status needed to be counted. I’ve done this in other languages before and had an algorithm which I had ported over to Python. But it dawned on me that there was a better way.

Sample Input & Output

Before we jump into the algorithms, here’s a sample input and output to help frame the following solutions.

records = [
{ "id": 1, "status": "Pass" },
{ "id": 2, "status": "Fail" },
{ "id": 3, "status": "Pass" },
{ "id": 4, "status": "Exempt" }…

Image for post
Image for post
Photo by Kelly Sikkema on Unsplash

A simple and free strategy to fight against the scalpers and bots

Don’t get the wrong idea from the featured image, I’m not advocating to consign your child to refresh duty.

If you’re anything like me then you’ve been locked up to varying degrees all year while anticipating all of the new tech that 2020 has to offer. Whether you’re interested in a Playstation 5, an XBox Series X, or next gen PC hardware, it doesn’t matter…you’re probably just as dejected and lost in the war against scalpers and bots as I am.

Well, fortunately I’ve found a solution to the problem that does not cost money, requires no technical skills, and will save me from the daily (hourly?) …


Installation to execution in 10 minutes

Image for post
Image for post
Photo by CDC on Unsplash

Moving from simple command-line scripts to web app development is a big leap in our Python journey. There are a variety of framework options with; however, even the concept can be overwhelming if you’ve never worked with a web framework.

If you’ve found this article then you’re probably researching different Python frameworks and trying to decide which to invest in. Instead of suffering from analysis paralysis, take a leap of faith and jump into Flask with this guide.

The first step is the hardest to take, but the easiest to make .We’ll …


A crucial next step for budding Pythonistas

Image for post
Image for post
Photo by Laurens Derks on Unsplash

It’s natural to focus — and get stuck on — syntax, structures, and libraries when learning a new programming language. Especially if it’s your first language, learning how to manage an environment is normally filed away under blissful ignorance.

It’s possible (somewhat) to get quite far without tackling environment management, but once you hit a wall that requires it, you’ll be left looking at a disorganized system folder that can best be described as a cluster****.

To start building good habits early, introduce Python virtual environments. …


The spiritual successor to NodeJS

Image for post
Image for post
Photo by Blanca Paloma Sánchez on Unsplash

Dating back to Node.js creator Ryan Dahl’s 2018 presentation 10 Things I Regret about Node.js, the programming community was anticipating his latest creation. Fast forward to May 2020 and Deno — A secure runtime for JavaScript and TypeScript — has released version 1.0.

Node.js as a JS runtime environment has become an absolute backbone for technology stacks. Any time you see the letter “N” in a tech stack, such as MEAN, that’s most likely Node.

With great success comes longevity and with longevity comes legacy challenges, some of which are too deeply ingrained in the architecture to be addressed.

This is where Deno comes in. If you’re curious about the language, as a seasoned developer or a fledgling self-starter who learned JavaScript, it’s undeniably exciting to get started on something that’s this new. …


Learn to use the range() function

Image for post
Image for post
Photo by Annie Spratt on Unsplash

There are a variety of reasons to count backwards in a program. Maybe you want to iterate over a list starting from the end or simply want to display a countdown timer.

We have multiple options available to us and are going to introduce two of those options: manually modifying a counter variable and using the range() function.

Manually Counting

The most basic method of counting backwards is to use a counting variable inside of a while loop. We need three definitions in order to count backwards: the starting point, how to exit the loop, and how to modify the loop.

Let’s set up a countdown timer from 10 to 0. We identify that the counter starts at 10, exits at 0, and the counter will be reduced by one after each loop. …


Convert a list to evenly sized segments

Image for post
Image for post
Photo by Martin Woortman on Unsplash

The Challenge

Create a function that converts a list to a two-dimensional “list of lists” where each nested structure is a specified equal length.

Here are some example inputs and expected outputs:

# INPUT LIST: [1,2,3,4,5,6]
# INPUT SIZE: 3
# OUTPUT: [[1,2,3],[4,5,6]]
# INPUT LIST: [1,2,3,4,5]
# INPUT SIZE: 2
# OUTPUT: [[1,2],[3,4],[5]]
# INPUT LIST: [1,2,3]
# INPUT SIZE: 4
# OUTPUT: [[1,2,3]]

Reviewing our examples, there are some important distinctions for scenarios where the list and chunk size do not perfect match up.

  • When the list does not evenly divide by the chunk size, the last index will be shorter than the evenly sized chunks. …

Use argparse to define script values

Image for post
Image for post
Photo by Cupcake Media on Unsplash

Python provides a native library for passing command-line values to scripts called argparse. With argparse, we can include variable values in the execution command instead of using input() to request the value mid-execution. This saves us time and more importantly allows script execution lines to be saved and conveniently used — either manually or automated.

If you’ve written any Python scripts that require one or two values via input(), then read on and consider implementing argparse to simplify the execution of your scripts. …


Python tutorial with code examples

Image for post
Image for post
Photo by Philipp Katzenberger on Unsplash

Whenever teaching programming, the analogy I use for copying a list is taking a photocopy. It’s a simple, visual example that everybody can relate to. But more importantly, the analogy has a major flaw — intentionally. Unlike a photocopy, there is an additional component to copying lists and any other complex data structure: shallow copying vs deep copying.

In this guide we’ll demonstrate both shallow and deep copying to highlight the difference. Shallow copying is a simpler process so we’ll provide three different techniques for doing so.

Starter Code

We’re going to need some initial starter code to get started. We’re defining a simple class with one attribute name. The __init__ method is executed when an instance of the class is created and the __repr__ method is responsible for defining when an instance of the class is printed. …


Reflections on wearing multiple hats

Image for post
Image for post
Photo by Alex on Unsplash

The last 24 months have been an interesting time. Filled with change, I’ve moved from Training to Professional Services, which has been redefined as Customer Success. I have one directive: make the customer successful. Hold my drink…

Pursuing this simple yet difficult goal, I find myself asking the same question over and over. Do I fish for the man or do I teach the man to fish? Well duh, dummy you teach them to fish. Except it’s not that easy because customers don’t want the small fish, they’re looking for whales.

Listening to Pulse 2018’s Great Debate: How Technical Should CSMs Be? I realized the answer, as any debate should illuminate, is somewhere in the middle. I’m fortunate to have complimentary skillsets — I’m not one-dimensional. …

About

Jonathan Hsu

Top writer with 1M+ views. Follow me at medium.com/code-85 and medium.com/growth-85

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