How to Sort a List of Tuples by the Second Element in Python

Sort a list of tuples by the second element in Python: Command Prompt output of sorted with a lambda key and sort with reverse=True

To sort a list of tuples by the second element in Python, pass a key function to sorted() or list.sort(): sorted(data, key=lambda t: t[1]). Add reverse=True for descending order, or use operator.itemgetter(1), which does the same thing slightly faster. This guide shows the basic sort, sorting by the second element and then another element, mixed … Read more >>

How to Transpose an Array in Python: 5 Methods with Examples

How to Transpose an Array in Python

If you’ve ever needed to flip rows and columns in your data, converting a 3×4 matrix into a 4×3 one, that’s exactly what transposing an array in Python. In this tutorial, I’ll walk you through five different ways to do it: using NumPy’s transpose() function, the .T shortcut, Python’s built-in zip(), list comprehension, and nested loops. I’ll also cover a … Read more >>

How to Find an Element’s Index in a Python Array

Find an Element’s Index in a Python Array

I recently built a small HR leave tracker for a 10-person team in Austin, Texas. The team previously tracked leave requests in Excel, but they needed a simple Python app with a local SQLite database. While processing requests, I often needed to find an employee, leave type, or approval status inside an array. Finding the … Read more >>

Python Generating Nested Dictionary Key Error

python keyerror

Recently, I was working on a data analysis project involving customer sales data from different US states. I needed to create nested dictionaries to organize sales information by state, city, and product categories. The challenge I faced was the dreaded KeyError that kept popping up whenever I tried to generate nested dictionary structures. This error … Read more >>

Print String with Quotes in Python

how to print quotation marks in python

While I was helping a junior developer on my team who was struggling with printing strings that contained quotes in Python. At first glance, it looks simple. But when you try to print a sentence like He said, “Python is amazing!”, Python throws errors if you don’t format it correctly. I’ve been working with Python … Read more >>

How to Build Perceptron in Python?

Mathematical Model of Perceptron

If you are a deep learning or machine learning enthusiast, you must know how to build Perceptron in Python, which is the foundation of creating AI, machine learning models, etc. I will explain the perceptron in depth with an example. You will understand how the perceptron works: it takes the input, processes the input, and … Read more >>

Check if a Variable Is Not Null in Python

python not null

I’ve often needed to verify if a variable is not null before proceeding with operations. This check is fundamental when working with data, user inputs, or APIs because null values can cause your program to crash or behave unexpectedly. In Python, the concept of “null” is represented by the special value None. Knowing how to … Read more >>

How to Convert a Dictionary to a String in Python

Convert a dict to a string in Python: Command Prompt output comparing str() and json.dumps() for the same dictionary

To convert a dict to a string in Python, use json.dumps(d) when the text will be stored, sent to an API or read by another program, and str(d) for a quick, Python-style text you only want to print or log. For a custom format such as key=value pairs, join the items yourself. This guide compares … Read more >>