Python

71 Python Code Snippets for Everyday Problems

If you’ve been following me for any amount of time, you know that I regularly publish Python code snippets for everyday problems. Well, I figured I’d finally aggregate all those responses in one massive article with links to all those resources.

Everyday Problems

In this section, we’ll take a look at various common scenarios that arise and how to solve them with Python code. Specifically, I’ll share a brief explanation of the problem with a list of Python code solutions. Then, I’ll link all the resources I have.

Inverting a Dictionary

Sometimes when we have a dictionary, we want to be able to flip its keys and values. Of course, there are concerns like “how do we deal with duplicate values?” and “what if the values aren’t hashable?” That said, in the simple case, there are a few solutions:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
# Use to invert dictionaries that have unique values
my_inverted_dict = dict(map(reversed, my_dict.items()))
 
# Use to invert dictionaries that have unique values
my_inverted_dict = {value: key for key, value in my_dict.items()}
 
# Use to invert dictionaries that have non-unique values
from collections import defaultdict
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}
 
# Use to invert dictionaries that have non-unique values
my_inverted_dict = dict()
for key, value in my_dict.items():
    my_inverted_dict.setdefault(value, list()).append(key)
 
# Use to invert dictionaries that have lists of values
my_dict = {value: key for key in my_inverted_dict for value in my_map[key]}

For more explanation, check out my article titled “How to Invert a Dictionary in Python.” It includes a breakdown of each solution, their performance metrics, and when they’re applicable. Likewise, I have a YouTube video which covers the same topic.

Summing Elements of Two Lists

Let’s say you have two lists, and you want to merge them together into a single list by element. In other words, you want to add the first element of the first list to the first element of the second list and store the result in a new list. Well, there are several ways to do that:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
ethernet_devices = [1, [7], [2], [8374163], [84302738]]
usb_devices = [1, [7], [1], [2314567], [0]]
 
# The long way
all_devices = [
    ethernet_devices[0] + usb_devices[0],
    ethernet_devices[1] + usb_devices[1],
    ethernet_devices[2] + usb_devices[2],
    ethernet_devices[3] + usb_devices[3],
    ethernet_devices[4] + usb_devices[4]
]
 
# Some comprehension magic
all_devices = [x + y for x, y in zip(ethernet_devices, usb_devices)]
 
# Let's use maps
import operator
all_devices = list(map(operator.add, ethernet_devices, usb_devices))
 
# We can't forget our favorite computation library
import numpy as np
all_devices = np.add(ethernet_devices, usb_devices)

If you’d like a deeper explanation, check out my article titled “How to Sum Elements of Two Lists in Python” which even includes a fun challenge. Likewise, you might get some value out of my YouTube video on the same topic.

Checking if a File Exists

One of the amazing perks of Python is how easy it is to manage files. Unlike Java, Python has a built-in syntax for file reading and writing. As a result, checking if a file exists is a rather brief task:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
# Brute force with a try-except block (Python 3+)
try:
    with open('/path/to/file', 'r') as fh:
        pass
except FileNotFoundError:
    pass
 
# Leverage the OS package (possible race condition)
import os
exists = os.path.isfile('/path/to/file')
 
# Wrap the path in an object for enhanced functionality
from pathlib import Path
config = Path('/path/to/file')
if config.is_file():
    pass

As always, you can learn more about these solutions in my article titled “How to Check if a File Exists in Python” which features three solutions and performances metrics.

Converting Two Lists Into a Dictionary

Previously, we talked about summing two lists in Python. As it turns out, there’s a lot we can do with two lists. For example, we could try mapping one onto the other to create a dictionary.

As with many of these problems, there are a few concerns. For instance, what if the two lists aren’t the same size? Likewise, what if the keys aren’t unique or hashable? That said, in the simple case, there are some straightforward solutions:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']
 
# Convert two lists into a dictionary with zip and the dict constructor
name_to_value_dict = dict(zip(column_names, column_values))
 
# Convert two lists into a dictionary with a dictionary comprehension
name_to_value_dict = {key:value for key, value in zip(column_names, column_values)}
 
# Convert two lists into a dictionary with a loop
name_value_tuples = zip(column_names, column_values)
name_to_value_dict = {}
for key, value in name_value_tuples:
    if key in name_to_value_dict:
        pass # Insert logic for handling duplicate keys
    else:
        name_to_value_dict[key] = value

Once again, you can find an explanation for each of these solutions and more in my article titled “How to Convert Two Lists Into a Dictionary in Python.” If you are a visual person, you might prefer my YouTube video which covers mapping lists to dictionaries as well.

Checking if a List Is Empty

If you come from a statically typed language like Java or C, you might be bothered by the lack of static types in Python. Sure, not knowing the type of a variable can sometimes be frustrating, but there are perks as well. For instance, we can check if a list is empty by its type flexibility—among other methods:

01
02
03
04
05
06
07
08
09
10
11
12
13
my_list = list()
 
# Check if a list is empty by its length
if len(my_list) == 0:
    pass  # the list is empty
 
# Check if a list is empty by direct comparison (only works for lists)
if my_list == []:
    pass  # the list is empty
 
# Check if a list is empty by its type flexibility **preferred method**
if not my_list:
    pass  # the list is empty

If you’d like to learn more about these three solutions, check out my article titled “How to Check if a List in Empty in Python.” If you’re in a pinch, check out my YouTube video which covers the same topic.

Cloning a List

One of my favorite subjects in programming is copying data types. After all, it’s never easy in this reference-based world we live, and that’s true for Python as well. Luckily, if we want to copy a list, there are a few ways to do it:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
my_list = [27, 13, -11, 60, 39, 15]
 
# Clone a list by brute force
my_duplicate_list = [item for item in my_list]
 
# Clone a list with a slice
my_duplicate_list = my_list[:]
 
# Clone a list with the list constructor
my_duplicate_list = list(my_list)
 
# Clone a list with the copy function (Python 3.3+)
my_duplicate_list = my_list.copy()  # preferred method
 
# Clone a list with the copy package
import copy
my_duplicate_list = copy.copy(my_list)
my_deep_duplicate_list = copy.deepcopy(my_list)
 
# Clone a list with multiplication?
my_duplicate_list = my_list * 1  # do not do this

When it comes to cloning, it’s important to be aware of the difference between shallow and deep copies. Luckily, I have an article covering that topic.

Finally, you can find out more about the solutions listed above in my article titled “How to Clone a List in Python.” In addition, you might find value in my related YouTube video titled “7 Ways to Copy a List in Python Featuring The Pittsburgh Penguins.”

Retrieving the Last Item of a List

Since we’re on the topic of lists, lets talk about getting the last item of a list. In most languages, this involves some convoluted mathematical expression involving the length of the list. What if I told you there is are several more interesting solutions in Python?

01
02
03
04
05
06
07
08
09
10
11
12
13
my_list = ['red', 'blue', 'green']
 
# Get the last item with brute force using len
last_item = my_list[len(my_list) - 1]
 
# Remove the last item from the list using pop
last_item = my_list.pop()
 
# Get the last item using negative indices *preferred & quickest method*
last_item = my_list[-1]
 
# Get the last item using iterable unpacking
*_, last_item = my_list

As always, you can learn more about these solutions from my article titled “How to Get the Last Item of a List in Python” which features a challenge, performance metrics, and a YouTube video.

Making a Python Script Shortcut

Sometimes when you create a script, you want to be able to run it conveniently at the click of a button. Fortunately, there are several ways to do that.

First, we can create a Windows shortcut with the following settings:

1
\path\to\trc-image-titler.py -o \path\to\output

Likewise, we can also create a batch file with the following code:

1
2
@echo off
\path\to\trc-image-titler.py -o \path\to\output

Finally, we can create a bash script with the following code:

1
2
#!/bin/sh
python /path/to/trc-image-titler.py -o /path/to/output

If you’re looking for more explanation, check out the article titled “How to Make a Python Script Shortcut with Arguments.”

Sorting a List of Strings

Sorting is a common task that you’re expected to know how to implement in Computer Science. Despite the intense focus on sorting algorithms in most curriculum, no one really tells you how complicated sorting can actually get. For instance, sorting numbers is straightforward, but what about sorting strings? How do we decide a proper ordering? Fortunately, there are a lot of options in Python:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
my_list = ["leaf", "cherry", "fish"]
 
# Brute force method using bubble sort
my_list = ["leaf", "cherry", "fish"]
size = len(my_list)
for i in range(size):
    for j in range(size):
        if my_list[i] < my_list[j]:
            temp = my_list[i]
            my_list[i] = my_list[j]
            my_list[j] = temp
 
# Generic list sort *fastest*
my_list.sort()
 
# Casefold list sort
my_list.sort(key=str.casefold)
 
# Generic list sorted
my_list = sorted(my_list)
 
# Custom list sort using casefold (>= Python 3.3)
my_list = sorted(my_list, key=str.casefold)
 
# Custom list sort using current locale
import locale
from functools import cmp_to_key
my_list = sorted(my_list, key=cmp_to_key(locale.strcoll))
  
# Custom reverse list sort using casefold (>= Python 3.3)
my_list = sorted(my_list, key=str.casefold, reverse=True)

If you’re curious about how some of these solutions work, or you just want to know what some of the potential risks are, check out my article titled “How to Sort a List of Strings in Python.”

Parsing a Spreadsheet

One of the more interesting use cases for Python is data science. Unfortunately, however, that means handling a lot of raw data in various formats like text files and spreadsheets. Luckily, Python has plenty of built-in utilities for reading different file formats. For example, we can parse a spreadsheet with ease:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Brute force solution
csv_mapping_list = []
with open("/path/to/data.csv") as my_data:
  line_count = 0
  for line in my_data:
    row_list = [val.strip() for val in line.split(",")]
    if line_count == 0:
      header = row_list
    else:
      row_dict = {key: value for key, value in zip(header, row_list)}
      csv_mapping_list.append(row_dict)
    line_count += 1
 
# CSV reader solution
import csv
csv_mapping_list = []
with open("/path/to/data.csv") as my_data:
    csv_reader = csv.reader(my_data, delimiter=",")
    line_count = 0
    for line in csv_reader:
        if line_count == 0:
            header = line
        else:
            row_dict = {key: value for key, value in zip(header, line)}
            csv_mapping_list.append(row_dict)
        line_count += 1
 
# CSV DictReader solution
import csv
with open("/path/to/dict.csv") as my_data:
    csv_mapping_list = list(csv.DictReader(my_data))

In this case, we try to get our output in a list of dictionaries. If you want to know more about how this works, check out the complete article titled “How to Parse a Spreadsheet in Python.”

Sorting a List of Dictionaries

Once you have a list of dictionaries, you might want to organize them in some specific order. For example, if the dictionaries have a key for date, we can try sorting them in chronological order. Luckily, sorting is another relatively painless task:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
csv_mapping_list = [
  {
    "Name": "Jeremy",
    "Age": 25,
    "Favorite Color": "Blue"
  },
  {
     "Name": "Ally",
     "Age": 41,
     "Favorite Color": "Magenta"
  },
  {
    "Name": "Jasmine",
    "Age": 29,
    "Favorite Color": "Aqua"
  }
]
 
# Custom sorting
size = len(csv_mapping_list)
for i in range(size):
    min_index = i
    for j in range(i + 1, size):
        if csv_mapping_list[min_index]["Age"] > csv_mapping_list[j]["Age"]:
            min_index = j
    csv_mapping_list[i], csv_mapping_list[min_index] = csv_mapping_list[min_index], csv_mapping_list[i]
 
# List sorting function
csv_mapping_list.sort(key=lambda item: item.get("Age"))
 
# List sorting using itemgetter
from operator import itemgetter
f = itemgetter('Name')
csv_mapping_list.sort(key=f)
 
# Iterable sorted function
csv_mapping_list = sorted(csv_mapping_list, key=lambda item: item("Age"))

All these solutions and more outlined in my article titled “How to Sort a List of Dictionaries in Python.”

Writing a List Comprehension

One of my favorite Python topics to chat about is list comprehensions. As someone who grew up on languages like Java, C/C++, and C#, I had never seen anything quite like a list comprehension until I played with Python. Now, I’m positively obsessed with them. As a result, I put together an entire list of examples:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Define a generic 1D list of constants
my_list = [2, 5, -4, 6]
 
# Duplicate a 1D list of constants
[item for item in my_list]
 
# Duplicate and scale a 1D list of constants
[2 * item for item in my_list]
 
# Duplicate and filter out non-negatives from 1D list of constants
[item for item in my_list if item < 0]
 
# Duplicate, filter, and scale a 1D list of constants
[2 * item for item in my_list if item < 0]
 
# Generate all possible pairs from two lists
[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]
 
# Redefine list of contents to be 2D
my_list = [[1, 2], [3, 4]]
 
# Duplicate a 2D list
[[item for item in sub_list] for sub_list in my_list]
 
# Duplicate an n-dimensional list
def deep_copy(to_copy):
    if type(to_copy) is list:
        return [deep_copy(item) for item in to_copy]
    else:
        return to_copy

As always, you can find a more formal explanation of all this code in my article titled “How to Write a List Comprehension in Python.” As an added bonus, I have a YouTube video which shares several examples of list comprehensions.

Merging Two Dictionaries

In this collection, we talk a lot about handling data structures like lists and dictionaries. Well, this one is no different. In particular, we’re looking at merging two dictionaries. Of course, combining two dictionaries comes with risks. For example, what if there are duplicate keys? Luckily, we have solutions for that:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
yusuke_power = {"Yusuke Urameshi": "Spirit Gun"}
hiei_power = {"Hiei": "Jagan Eye"}
powers = dict()
 
# Brute force
for dictionary in (yusuke_power, hiei_power):
    for key, value in dictionary.items():
        powers[key] = value
 
# Dictionary Comprehension
powers = {key: value for d in (yusuke_power, hiei_power) for key, value in d.items()}
 
# Copy and update
powers = yusuke_power.copy()
powers.update(hiei_power)
 
# Dictionary unpacking (Python 3.5+)
powers = {**yusuke_power, **hiei_power}
 
# Backwards compatible function for any number of dicts
def merge_dicts(*dicts: dict):
    merged_dict = dict()
    for dictionary in dicts:
        merge_dict.update(dictionary)
    return merged_dict

If you’re interested, I have an article which covers this exact topic called “How to Merge Two Dictionaries in Python” which features four solutions as well performance metrics.

Formatting a String

Whether we like to admit it or not, we often find ourselves burying print statements throughout our code for quick debugging purposes. After all, a well placed print statement can save you a lot of time. Unfortunately, it’s not always easy or convenient to actually display what we want. Luckily, Python has a lot of formatting options:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
name = Jeremy
age = 25
 
# String formatting using concatenation
print("My name is " + name + ", and I am " + str(age) + " years old.")
 
# String formatting using multiple prints
print("My name is ", end="")
print(name, end="")
print(", and I am ", end="")
print(age, end="")
print(" years old.")
 
# String formatting using join
print(''.join(["My name is ", name, ", and I am ", str(age), " years old"]))
 
# String formatting using modulus operator
print("My name is %s, and I am %d years old." % (name, age))
 
# String formatting using format function with ordered parameters
print("My name is {}, and I am {} years old".format(name, age))
 
# String formatting using format function with named parameters
print("My name is {n}, and I am {a} years old".format(a=age, n=name))
 
# String formatting using f-Strings (Python 3.6+)
print(f"My name is {name}, and I am {age} years old")

Keep in mind that these solutions don’t have to be used with print statements. In other words, feel free to use solutions like f-strings wherever you need them.

As always, you can find an explanation of all these solutions and more in my article titled “How to Format a String in Python.” If you’d rather see these snippets in action, check out my YouTube video titled “6 Ways to Format a String in Python Featuring My Cat.”

Printing on the Same Line

Along a similar line as formatting strings, sometimes you just need to print on the same line in Python. As the print command is currently designed, it automatically applies a newline to the end of your string. Luckily, there are a few ways around that:

1
2
3
4
5
6
7
8
9
# Python 2 only
print "Live PD",
 
# Backwards compatible (also fastest)
import sys
sys.stdout.write("Breaking Bad")
 
# Python 3 only
print("Mob Psycho 100", end="")

As always, if you plan to use any of these solutions, check out the article titled “How to Print on the Same Line in Python” for additional use cases and caveats.

Testing Performance

Finally, sometimes you just want to compare a couple chunks of code. Luckily, Python has a few straightforward options:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
# Brute force solution
import datetime
start_time = datetime.datetime.now()
[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)] # example snippet
end_time = datetime.datetime.now()
print end_time - start_time
 
# timeit solution
import timeit
min(timeit.repeat("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]"))
 
# cProfile solution
import cProfile
cProfile.run("[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]")

Again, if you want more details, check the article titled “How to Performance Test Python Code.”

Share Your Own Problems

As you can see, this article and its associated series is already quite large. That said, I’d love to continue growing them. As a result, you should consider sharing some of your own problems. After all, there has be something you Google regularly. Why not share it with us?

Published on Web Code Geeks with permission by Jeremy Grifski, partner at our WCG program. See the original article here: 71 Python Code Snippets for Everyday Problems

Opinions expressed by Web Code Geeks contributors are their own.

Jeremy Grifski

Jeremy is the founder of The Renegade Coder, a software curriculum website launched in 2017. In addition, he is a PhD student with an interest in education and data visualization.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Yolanda
Yolanda
2 years ago

Hi,
Thanks for this nice article.
I’m learning Python and my code has a list of class.
My problem is the conversion strings to write the list content in a .txt file.
Any suggestions are welcome.

Jeremy
2 years ago
Reply to  Yolanda

Hey Yolanda! I’m not sure exactly what you’re asking, but you’re welcome to follow up with me on Discord: https://discord.gg/Jhmtj7Z

Xhevo
Xhevo
1 year ago

Thanks!

Back to top button