How to Remove Multiple Keys from a Dictionary in Python

Python remove multiple keys from dictionary: Command Prompt output of a pop loop removing private fields

To remove multiple keys from a dictionary in Python, loop over the keys and call d.pop(key, None) for each one. The None default means missing keys are skipped instead of raising KeyError. To keep the original intact, build a new dict with a comprehension: The examples strip private fields from US customer and order records, … Read more >>

How to Split an Array in TypeScript

TypeScript split array: Command Prompt output of a generic chunk function splitting emails into batches

To split an array in TypeScript, use slice(): arr.slice(0, i) gives the part before index i and arr.slice(i) gives the rest, without changing the original. To split into chunks of N items, call slice in a loop: For the outputs on this page I used TypeScript 7.0.2 on Node v22.22.2 with strict checks, so the … Read more >>

How to Write Bytes to a File in Python

Python write bytes to file: Command Prompt output of writing encoded invoice bytes in wb mode and reading them back

To write bytes to a file in Python, open it in binary write mode with open(path, “wb”) and call f.write(data), where data is a bytes or bytearray object. For a one-liner, Path(path).write_bytes(data) does the same: Binary mode works the same on every OS, but I ran these on Windows with Python 3.12.5, where text-mode newline … Read more >>

Python break and continue: Exit or Skip a Loop

Python break and continue: Command Prompt output of a for loop skipping cancelled orders with continue

Python’s break and continue both change how a loop runs. break exits the loop immediately, and continue skips the rest of the current pass and moves on to the next one. Both work in for and while loops. The orders, statuses and seat numbers below are made up, but every output is real: I ran … Read more >>

How to Use the TypeScript forEach Method on Arrays

TypeScript forEach method: Command Prompt output listing orders with their index and total revenue

The TypeScript forEach method calls a function once for every element of an array: orders.forEach((order, index) => { … }). TypeScript infers the element type for the callback, and forEach always returns void, so use it for side effects such as logging or totals: My setup for these examples: TypeScript 7.0.2 and Node.js v22.22.2, compiled … Read more >>

How to Read a Specific Line from a Text File in Python

Python read specific line from file: Command Prompt output comparing readlines, islice and linecache

To read a specific line from a text file in Python, loop over the file with enumerate(f, start=1) and return the line when the counter matches. It streams the file, so size doesn’t matter. linecache.getline(path, n) is the one-call option: To check the behaviour of each method, including what happens past the end of the … Read more >>

How to Convert Decimal Numbers to Binary in Python

Python decimal to binary: Command Prompt output of bin, format and f-string conversions of 156

To convert a decimal number to binary in Python, use bin(n), which returns a string like ‘0b10011100’, or format(n, “b”) and f”{n:b}” for the digits without the 0b prefix. Add a width such as 08b to pad with zeros: Binary strings are easy to get subtly wrong, so every value here was printed by Python … Read more >>

How to Use the Array reduce() Method in TypeScript

TypeScript array reduce method: Command Prompt output tracing the accumulator at every step

The array reduce() method in TypeScript runs a callback on each element and carries one value, the accumulator, from step to step, so an array becomes a single result. Pass an initial value as the second argument; its type decides what reduce() returns: All of the output on this page is real: TypeScript 7.0.2 plus … Read more >>

How to Convert an Object to an Array in TypeScript

TypeScript convert object to array: Command Prompt table of store objects built with Object.entries and map

To convert an object to an array in TypeScript, use Object.values(obj) for the values, Object.keys(obj) for the property names, or Object.entries(obj) for [key, value] pairs. Each returns a new array and leaves the object alone: These examples were compiled with –strict and run with TypeScript 7.0.2 on Node.js v22.22.2; nothing below is hand-typed output. You’ll … Read more >>

How to Convert a String to an Array in TypeScript

TypeScript convert string to array: Command Prompt output comparing split and Array.from on a string with emoji

To convert a string to an array in TypeScript, use split() with a separator, such as “a,b,c”.split(“,”), which returns a string[]. For an array of single characters, use Array.from(str) instead of split(“”): Here I’m compiling in strict mode with TypeScript 7.0.2 with Node.js v22.22.2, and each output block is pasted from the console. You’ll see … Read more >>