Python dict() Function: A Practical Guide With Examples

Python dict() Function

When I build a small reporting script, I often need a fast way to organize related details. For example, an employee record may need a name, department, email, and monthly sales figure. Keeping those values in separate variables becomes messy quickly. That is where the Python dict() function helps. It creates a dictionary, which stores … Read more >>

How to Write JSON Data to a File in Python

Python write JSON to file: Command Prompt output of json.dump writing an indented orders.json file

To write JSON data to a file in Python, open the file in write mode and call json.dump(data, f). Add indent=2 for readable output, and open the file with encoding=”utf-8″ so names with accents survive: These snippets ran on Python 3.12.5 in a Windows Command Prompt, where encoding mistakes show up fast. You’ll see json.dump … Read more >>

How to Add Elements to an Array in TypeScript

Add elements to an array in TypeScript: Command Prompt output of push, unshift and splice adding cities to a string array

To add elements to an array in TypeScript, call push() to append to the end, unshift() to add to the beginning, or splice(index, 0, value) to insert at a position in the middle. Those three change the array you already have. If you need a new array instead and want the original left alone, use … Read more >>

How to Remove an Item from an Array in TypeScript

Remove an item from an array in TypeScript: Command Prompt output of filter, splice, shift and pop on an array of cities

To remove an item from an array in TypeScript, use filter() when you want a new array without the value, and splice(index, 1) when you want to remove it from the array you already have. shift() drops the first element, pop() drops the last, and delete should be avoided because it leaves an empty slot … Read more >>

How to Use the TypeScript Array splice() Method

TypeScript array splice: Command Prompt output removing, inserting and replacing route stops with splice

The TypeScript array splice() method changes an array in place: arr.splice(start, deleteCount, …items) removes deleteCount elements at start, inserts any items there, and returns the removed elements as a new array. Use toSpliced() when you need a copy instead: My test bench was TypeScript 7.0.2 with Node v22.22.2, strict mode on, and the printed arrays … Read more >>

How to Remove Undefined Values from an Array in TypeScript?

Remove Undefined Values from an Array in TypeScript

Recently, I worked with arrays in TypeScript that contained undefined elements. We wanted to remove these undefined values from the array. In this tutorial, I will explain how to remove undefined values from an array in TypeScript using different methods. We will ensure to filter out undefined values and ensure our arrays only contain valid … Read more >>

How to Convert JSON to Array in TypeScript?

Min() function in python

One of my team members recently asked about converting JSON to array in TypeScript. There are various methods to do this. In this tutorial, I will explain how to convert JSON to an array in TypeScript using various methods with examples. JSON (JavaScript Object Notation) is a lightweight data-interchange format that’s easy for humans to … Read more >>

Get Unique Values from an Array of Objects in TypeScript

Get unique values from an array of objects in TypeScript: Command Prompt output of distinct cities and customers pulled out with a Set

To get unique values from an array of objects in TypeScript, map the property you want and wrap it in a Set: […new Set(orders.map(order => order.city))] gives you every distinct city, in the order they first appeared. That works because a Set compares primitives by value. Objects are a different story: a Set compares them … Read more >>

Remove Duplicates from an Array of Objects in TypeScript

Remove duplicates from an array of objects in TypeScript: Command Prompt output of four orders reduced to three unique rows by id

To remove duplicates from an array of objects in TypeScript, key them with a Map: […new Map(orders.map(o => [o.id, o])).values()] gives you one row per id, in the order they first appeared. A Set will not do it, because it compares objects by reference rather than by contents. This guide covers deduplicating by one property, … Read more >>

Remove Empty Strings from an Array in JavaScript and TypeScript

Remove empty strings from an array: Command Prompt output of filter(Boolean) and an explicit filter on an array of city names

To remove empty strings from an array, filter them out: values.filter(Boolean) is the short version and values.filter(value => value !== “”) is the explicit one. Both return a new array and leave the original alone. If the array also contains strings that are only spaces or tabs, trim first: values.filter(value => value.trim() !== “”). The … Read more >>