How to Iterate Through a List Backward in Python

Python iterate through a list backward: Command Prompt output of reversed() printing orders newest first

To iterate through a list backward in Python, wrap it in the built-in reversed() function: for item in reversed(my_list):. It walks from the last item to the first, doesn’t copy the list and doesn’t change it: I tested all of this on Python 3.12.5 under Windows, so each output block is pasted straight from a … Read more >>

How to Slice Lists in Python

Python slice list: Command Prompt output of months sliced with start, stop and step values

To slice a list in Python, write my_list[start:stop:step]. You get a new list that begins at index start, stops just before stop, and moves step items at a time. Every part is optional, so [:3] means the first three items: Every output below comes from real runs on Python 3.12.5, with 2026 months, US cities … Read more >>

How to Generate a List of Random Numbers in Python

Python list of random numbers: Command Prompt output of a randint list comprehension with a fixed seed

To generate a list of random numbers in Python, put random.randint() inside a list comprehension: [random.randint(1, 100) for _ in range(10)] gives you ten whole numbers from 1 to 100. Need them all different? Use random.sample(range(1, 101), 10) instead: I ran every snippet here on Python 3.12.5 with NumPy 2.5.3, and each one calls random.seed() … Read more >>

How to Print a List Without Brackets in Python

Python print list without brackets: Command Prompt output of print(*cities, sep) next to the default bracketed list

To print a list without brackets in Python, unpack it into print() with a star: print(*my_list). Each item becomes its own argument, so Python prints them separated by spaces with no brackets, commas or quotes. Add sep to choose the separator: Each snippet ran under Python 3.12.5 in a Windows Command Prompt window, so the … Read more >>

How to Sort a List of Objects by Attribute in Python

Python sort list of objects by attribute: Command Prompt output of employees sorted by salary with a lambda key

To sort a list of objects by attribute in Python, pass a key function to sorted(): sorted(items, key=lambda x: x.attr) returns a new list ordered by that attribute. Use items.sort(key=…) to sort in place, and add reverse=True for descending order: Every output below is real, captured from a Windows Command Prompt running Python 3.12.5. In … Read more >>

How to Filter Lists in Python

Python filter list: Command Prompt output of a list comprehension keeping orders of $100 or more

To filter a list in Python, use a list comprehension with an if clause: [x for x in items if condition] builds a new list holding only the items that pass. The built-in filter(function, items) does the same job lazily, so wrap it in list(): My test machine for this page had Python 3.12.5; each … Read more >>

How to Convert String to Double in TypeScript (Number vs parseFloat)

Convert string to double in TypeScript: Command Prompt output comparing Number() and parseFloat()

To convert a string to a double in TypeScript, pass it to Number() or parseFloat(). TypeScript has no separate double type; every number is already a 64-bit double, so “1299.99” becomes 1299.99: My test bench for this one: TypeScript 7.0.2 and Node.js v22.22.2, strict mode on, with each console result below copied from a real … Read more >>

How to Convert an Integer to a String in Python

Convert integer to string in Python: Command Prompt output of str, f-string, format and percent

The quickest way to convert an integer to a string in Python is the built-in str() function: str(42) gives you ’42’. When the number goes inside a sentence, an f-string does the conversion for you: Nothing here needs a package; it’s plain Python 3.12.5, and I captured each run in a Windows Command Prompt. Below … Read more >>

TypeScript Enum with Multiple Values (4 Patterns That Work)

TypeScript enum with multiple values: Command Prompt output of a Record lookup giving each enum member a label, price and delivery days

A TypeScript enum with multiple values isn’t something the language supports directly: each member holds exactly one number or string. The clean fix is to keep the enum for the name and attach the extra values with a typed lookup object, Record<YourEnum, Details>: My test bench for this one was TypeScript 7.0.2 and Node.js v22.22.2 … Read more >>

How to Merge Maps in TypeScript (Spread, Loops and Generics)

TypeScript merge maps: Command Prompt output of new Map([...austin, ...denver]) combining two inventory Maps

The quickest way to handle a TypeScript merge maps task is spreading both Maps into a new one. Each Map is an iterable of [key, value] pairs, so new Map([…a, …b]) combines them, and the second Map wins on duplicate keys: Every snippet in this guide went through strict-mode tsc and then Node (TypeScript 7.0.2 … Read more >>