Python List Comprehension If Else: Where Each One Goes

Python list comprehension if else: the Command Prompt showing labels from a conditional expression and a shorter filtered list

In a Python list comprehension, if else goes before the for, and a bare if goes after it. Get that backwards and Python raises a SyntaxError. The first one labels every item. The second one throws items away. That single difference is what most of the questions about this are really about. I ran each … Read more >>

Matplotlib Transparent Background: savefig, Patches and Legends

Matplotlib transparent background: the same chart composited onto a dark panel and a warm one, showing through both

To save a Matplotlib chart with a transparent background, pass transparent=True to savefig: That clears the figure background and the axes background in the saved file. It changes nothing on screen, which is why people think it didn’t work. Below I check every claim by reading the alpha channel back out of the saved PNG, … Read more >>

torch.cat in PyTorch: Joining Tensors Along a Dimension

torch.cat in PyTorch: the Command Prompt showing two tensors joined along dim 0 and dim 1 with the resulting shapes

torch.cat joins a sequence of tensors along a dimension that already exists. The shapes have to match everywhere except that one dimension, and the result never gains an axis. That last part is the entire difference between torch.cat and torch.stack, which does add an axis. The runs below are on PyTorch 2.14.0 on Python 3.12.5, … Read more >>

React Component Not Rendering: 7 Causes and the Fix for Each

React component not rendering: a browser showing one component rendered and a lower-case one silently skipped

A React component not rendering is almost never a React bug. In the great majority of cases it is one of a handful of ordinary mistakes: the tag starts with a lower-case letter, the function forgot its return, the import does not match the export, && put a 0 on the page instead of your … Read more >>

React Data Grid Component: MUI X DataGrid and the Alternatives

React data grid component: the MUI X DataGrid rendering four order rows with sortable columns and pagination

A data grid is a table that sorts, filters, paginates and selects rows for you. In React the usual choice is MUI X DataGrid, whose Community version is free and MIT licensed, and this guide builds a working grid with it: columns, pagination, sorting, checkbox selection, formatted and custom cells. It also answers the question … Read more >>

React Error Boundaries with Functional Components

React error boundary with functional components: the browser showing a fallback message while the rest of the dashboard keeps working

To use a React error boundary with functional components, you still need one class: React has no hook that catches rendering errors, so the boundary itself has to be a class component even in an app made entirely of function components. You write that class once and wrap your function components in it. The usual … Read more >>

Merge Arrays of Objects in TypeScript (concat, spread, by id)

Merge arrays of objects in TypeScript: Command Prompt output of two order arrays combined with concat and spread

To merge two arrays in TypeScript, use concat() or spread syntax: a.concat(b) and […a, …b] both return a new array and leave the originals alone. That is all you need when the rows are independent. When both arrays hold objects that describe the same records, you usually want one row per id instead of two, … Read more >>

How to Initialize an Array in TypeScript

Initialize an array in TypeScript: Command Prompt output of typed arrays declared with values, empty and with the generic Array syntax

To initialize an array in TypeScript, declare the element type and give it a value: const cities: string[] = [“Austin”, “Denver”], or const ids: string[] = [] to start empty. Array<string> means the same as string[]. For a fixed number of slots use Array.from({ length: n }, …) rather than new Array(n), which leaves holes. … Read more >>

Check if an Array Is Null or Empty in TypeScript

Check if an array is null or empty in TypeScript: Command Prompt output showing the check against a filled array, an empty array, null and undefined

To check if an array is null or empty in TypeScript, write if (!items || items.length === 0), or the shorter if (!items?.length), which covers null, undefined and [] in one test. The order matters: reading .length before the null check throws at runtime. This guide shows both forms running against all four cases, the … Read more >>

Find the Maximum Value in an Array of Objects in TypeScript

Find the maximum value in an array of objects in TypeScript: Command Prompt output of the highest salary and the employee who earns it

To find the maximum value in an array of objects in TypeScript, decide first what you want back. For just the number, map the property and use Math.max(): Math.max(…employees.map(e => e.salary)). For the whole object that holds it, use reduce() and keep whichever item is larger. Both are one line, and both have an edge … Read more >>