How to Get or Select Multiple Keys from a Dictionary in Python

Get multiple keys from a Python dictionary with a dict comprehension, itemgetter and set operations

Python has no built-in syntax like d[“a”, “b”] for reading several keys at once (that would look up the tuple key (“a”, “b”)). Use a list comprehension to get the values, [d[k] for k in keys], or a dictionary comprehension to get a smaller dictionary, {k: d[k] for k in keys}. This guide covers those, … Read more >>

Create an Empty Dictionary in Python ({} vs dict())

Create an empty dictionary in Python: the Command Prompt showing dict.fromkeys giving every key the same list object

Creating an empty dictionary in Python takes one pair of braces: Both give you a real dictionary with no keys in it. The braces are the common choice, and they are marginally faster, but the two are interchangeable. What follows covers filling it afterwards, starting one with keys already in place, and the three ways … Read more >>

Fix SyntaxError: invalid syntax in Python

invalid syntax

When I started working with Python, I often ran into the dreaded SyntaxError: invalid syntax message. It was frustrating because sometimes the error didn’t make sense at first glance. I felt like my code looked perfectly fine, but Python kept complaining. Over the years, I’ve learned that syntax errors are usually caused by small mistakes, … Read more >>

Iterate Through a Dictionary in Python (keys, values, items)

Iterate through a dictionary in Python: the Command Prompt showing the RuntimeError raised when a dictionary changes size during iteration

To iterate through a dictionary in Python and get both parts, loop over .items(): Looping over the dictionary on its own gives you the keys and nothing else, which is the detail most people trip over first. Below: the four ways to loop, sorting as you go, nested dictionaries, and the RuntimeError that appears when … Read more >>

Append to a Dictionary in Python (There Is No append Method)

Python dictionary append: the Command Prompt showing the AttributeError that a dict object has no attribute append

Python dictionaries have no .append() method. You add to one by assigning to a key: Calling .append() on a dictionary raises AttributeError, because that method belongs to lists. A list adds to the end; a dictionary needs you to say which key. What follows covers adding one key, adding several, merging two dictionaries, and the … Read more >>

Check if an Array Contains a Value in Python (in operator)

Check if an array contains a value in Python: the Command Prompt showing the in operator returning True and False

Checking whether a Python array contains a value is done with the in operator: Note the vocabulary. What most people call an array, Python calls a list, and in works on both that and a NumPy array. There is no includes() or contains() method. Below: conditions rather than exact values, checking several at once, NumPy … Read more >>

Difference Between {} and [] in Python

Difference Between {} and [] Python

When I build a small reporting script, I often need two very different things: a sequence of report rows and a quick way to look up details by an ID. Python uses [] for one of those jobs and {} for the other, so choosing the wrong brackets can cause confusing errors later. The difference between {} and [] in Python looks small … Read more >>

Lists, Tuples, Sets, and Dictionaries in Python

Lists, Tuples, Sets, and Dictionaries Python

When I build a small inventory reporting script, I often start with a simple list of product names. Then the script grows. I need fixed settings, unique product codes, and quick lookups for stock levels. That is when choosing the right Python data structure starts to matter. Python gives you lists, tuples, sets, and dictionaries … Read more >>

Python bool() Function: A Practical Guide With Examples

Python bool() Function

When I build small reporting scripts, one common job is deciding whether the script should continue. Does the CSV contain rows? Did the API return data? Did a user enter a value? The Python bool() function gives a clean, reliable answer: True or False. You will use bool() constantly in if statements, loops, input checks, … Read more >>