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 >>

Check if Two Dictionaries Are Equal in Python

Python Check If Two Dictionaries are Equal using == operator

I was working on a project where I had to compare two large dictionaries in Python. These dictionaries contained employee records from different departments, and I needed to quickly check if they were the same. At first, I thought it would be as simple as comparing two Python lists, but dictionaries are unordered collections, so … Read more >>

Use Python While Loop with Assignment

python while with assignment

Recently, I was working on a project where I had to repeatedly check values from a database until a condition was met. The challenge was that I needed to assign values inside the loop while still controlling the exit condition. That’s when I realized that many Python beginners struggle with using while loops with assignment.So … Read more >>

How to Copy a Python Dictionary Without One Key?

python copy dict without one key

When I first started working with Python dictionaries, I often needed to create a copy but exclude one or two keys. At first, I thought Python would have a built-in method for this. However, just as in Excel, where you sometimes need a workaround, in Python, you also need to employ different techniques. Over the … Read more >>

How to Get Last Number in Python String

Python find last number in string using re.match() method

I was working on a project where I had to clean up messy text data coming from customer feedback forms. Some of these strings contained numbers mixed with words, and I specifically needed to get last number in Python string. At first, I thought there would be a built-in function, but there isn’t. So, I … Read more >>

Compare Two Lists in Python: Non-Matches, Duplicates and Order

Compare two lists in Python: the Command Prompt showing set lookups running thousands of times faster

To compare two lists in Python and return the non-matching elements, the shortest answer is the symmetric difference of two sets: Pick the second form when you need to know which list an item came from. Pick the first when you just want everything that differs. One warning up front: sets discard duplicates, so both … Read more >>

Convert a Nested Dictionary to JSON in Python

python dict to json

While working on a project where I had to send structured data from a Python backend to a web application. The data was stored in a nested dictionary, and I needed to convert it into JSON format. The issue is… while converting a simple dictionary to JSON is easy, many people get confused when the … Read more >>

Python Get Current Directory: os.getcwd() and Path.cwd()

Python get current directory: Command Prompt showing the working directory differs from the script folder so open fails

Call os.getcwd() to get the current directory in Python. It returns the working directory as a string, and Path.cwd() from pathlib gives you the same folder as a Path object: One catch trips up almost everyone: that’s the folder you launched Python from, not the folder your script sits in. I’ll show both, with real … Read more >>

Remove Empty Keys from a Python Dictionary

removing keys from dictionary python

As a developer, I was working on a data-cleaning project where I had to process a large JSON dataset from an API. The dataset represented customer information, but many records contained empty keys that served no purpose. If you’ve ever worked with dictionaries in Python, you know they’re one of the most powerful data structures. … Read more >>