How to Convert a Dictionary to a List in Python

Python dict to list: Command Prompt output of list() on a dictionary's keys, values and items

To convert a dictionary to a list in Python, call list() on the part you want: list(d) gives the keys, list(d.values()) the values, and list(d.items()) a list of (key, value) tuples. All three keep the dictionary’s insertion order: A small store price list runs through every example; I ran them on Python 3.12.5, where dicts … Read more >>

How to Get the Last Element in a List in Python

Python get last element in list: Command Prompt output of negative indexing on a list of order numbers

To get the last element of a list in Python, use the index -1: orders[-1]. Negative indexes count from the end, so -2 is the second to last. On an empty list [-1] raises IndexError, so check first when the list might be empty: The examples use order numbers and an event log; their output … Read more >>

How to Replace Items in a Python List

Python replace item in list: Command Prompt output of replacing list items by index and by slice

To replace items in a Python list, assign to an index: cities[1] = “Dallas”. To replace by value, find the position with index() first, and replace every match with a list comprehension: US city lists, a shopping cart and order statuses keep the examples concrete; each was run on Python 3.12.5. You’ll see replacement by … Read more >>

How to Shuffle a List in Python

Python shuffle list: Command Prompt output of random.shuffle returning None and random.sample making a shuffled copy

To shuffle a list in Python, call random.shuffle(my_list). It reorders the list in place and returns None, so don’t assign the result. For a shuffled copy that leaves the original alone, use random.sample(my_list, k=len(my_list)): I seeded the generator in most examples so the printed order matches the screenshots; the outputs come from Python 3.12.5. You’ll … Read more >>

How to Convert a String to Boolean in TypeScript

TypeScript string to boolean: Command Prompt output showing Boolean("false") returns true

To convert a string to boolean in TypeScript, compare the text: value.trim().toLowerCase() === “true”. Don’t use Boolean(value) or !!value: they test whether the string is empty, so Boolean(“false”) is true: Truthiness rules are easy to misremember, so I let TypeScript 7.0.2 and Node v22.22.2 settle each case; the env-var demo sets DEBUG on the same … Read more >>

How to Use the pow() Method in Python

Python pow method: Command Prompt output of compound interest on $10,000 calculated with pow

The pow() method in Python raises a number to a power: pow(base, exp) gives the same result as base ** exp. Its extra trick is a third argument, pow(base, exp, mod), which computes the remainder efficiently without building the huge intermediate number: I checked each result type and error message on Python 3.12.5, since pow() … Read more >>

How to Fix IndexError: tuple index out of range in Python

Python IndexError tuple index out of range: Command Prompt traceback from indexing past the end of a tuple

IndexError: tuple index out of range means you asked for a position the tuple doesn’t have. A tuple of length n has indexes 0 to n – 1 only. Fix it by checking len() first, looping without indexes, or handling empty tuples: The tracebacks here are copied from Python 3.12.5, so the arrows under the … Read more >>

How to Find the Length of a Tuple in Python

Python length of tuple: Command Prompt output of len() on store, empty and one-element tuples

To find the length of a tuple in Python, pass it to len(): len((“Austin”, “TX”, 78701)) returns 3. It counts the top-level items only, so a nested tuple counts as one item, and it works the same for empty and single-item tuples: Every result below was printed by Python 3.12.5, including the two TypeErrors that … Read more >>

How to Write a Dictionary to a File in Python

Python write dictionary to file: Command Prompt output of a price dictionary saved as key: value lines

To write a dictionary to a file in Python, pick a format first: json.dump(d, f) for data other programs read, a key: value line per item for plain text, csv.DictWriter for a list of dicts, or pickle.dump() for Python-only data: The real question is what you get back when you read the file, so every … Read more >>

Do-While Loop in Python (With Examples)

Do-while loop in Python: Command Prompt output showing a while loop that never runs and a while True loop that runs once

Do-while loops don’t exist in Python, but a while True loop that checks the condition at the bottom and exits with break does the same job. The body always runs at least once, just like a do-while in C, Java or JavaScript. The screenshots come from Python 3.12.5 in a real Command Prompt, typed answers … Read more >>