Check if a Key Exists in a Python Dictionary (in, get and More)

Python dictionary contains: Command Prompt showing get returning None for an existing key and the in operator

To check whether a Python dictionary contains a key, use in: "state" in customer returns True or False. It checks keys, not values, and it’s the fastest way to ask: Values, get(), several keys at once and nested JSON each have a catch, so I’ll walk through them with real output from Python 3.14.7. Check … Read more >>

Convert a String to a Byte Array in Python

python string to bytes

Recently, I was working on a Python project where I had to send data over a network. The API required the data in byte format instead of a normal string. That’s when I realized many beginners struggle with converting strings to byte arrays in Python. While it looks complex at first, Python actually makes this … Read more >>

How to Remove an Element from a List in Python

How to Remove an Element from the List in Python using remove() method

While working on a data-cleaning project for a retail company in the USA, I had to remove certain unwanted values from a Python list. At first glance, this sounds like a simple task. But as I dug deeper, I realized there are multiple ways to remove an element from a list in Python. Each method … Read more >>

How to Count Words in a File (and a String) in Python

Count words in a file in Python: Command Prompt output showing line, word and character counts for a reviews file

Python doesn’t need a library for this. To count words in a file in Python, read the file into a string, call split() with no arguments, and take len() of the list. Every run of spaces, tabs and newlines counts as one break: Below I’ll use a small file of US customer reviews, plus a … 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 >>

How to Increment a Value in a Python Dictionary

Python dictionary increment value: Command Prompt output of get() counting orders and USD totals by state

To increment a value in a Python dictionary, write d[key] += 1 when the key already exists. If it might be missing, use d[key] = d.get(key, 0) + 1, which starts new keys at zero instead of raising a KeyError: Every output here is from a real run on Python 3.12.5, using page visits, store … Read more >>

Append One List to Another in Python Without Nesting

Python Append List to List Without Nesting Using for loop and append() Method

When I first started working with Python lists more than a decade ago, one of the most confusing things I faced was appending one list to another. Over the years, I’ve learned multiple ways to append one list to another in Python without nesting. In this tutorial, I’ll walk you through these methods step by … Read more >>