DEV Community

Cover image for 71 Python Code Snippets for Everyday Problems
Jeremy Grifski
Jeremy Grifski

Posted on • Originally published at therenegadecoder.com on

71 Python Code Snippets for Everyday Problems

If this is your first time here, I'd like to encourage you to head over to the original post which is regularly maintained. At this point, it features over 100 snippets which are organized by category and complexity. In addition, there's a nice table of contents that can help you jump around.

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.

As a heads up, I’m looking to start porting all of the code snippets in this article to Jupyter Notebooks. If you’re interested in that kind of project, head on over to the GitHub repo. I’d appreciate the help!

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:

my_dict = {
  'Izuku Midoriya': 'One for All', 
  'Katsuki Bakugo': 'Explosion', 
  'All Might': 'One for All', 
  'Ochaco Uraraka': 'Zero Gravity'
}

# 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_inverted_dict[key]}
Enter fullscreen mode Exit fullscreen mode

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:

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)
Enter fullscreen mode Exit fullscreen mode

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:

# 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
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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?

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
Enter fullscreen mode Exit fullscreen mode

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:

\path\to\trc-image-titler.py -o \path\to\output
Enter fullscreen mode Exit fullscreen mode

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

@echo off
\path\to\trc-image-titler.py -o \path\to\output
Enter fullscreen mode Exit fullscreen mode

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

#!/bin/sh
python /path/to/trc-image-titler.py -o /path/to/output
Enter fullscreen mode Exit fullscreen mode

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:

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)
Enter fullscreen mode Exit fullscreen mode

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:

# 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))
Enter fullscreen mode Exit fullscreen mode

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:

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.get("Age"))
Enter fullscreen mode Exit fullscreen mode

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:

# 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
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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:

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")
Enter fullscreen mode Exit fullscreen mode

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:

# 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="")
Enter fullscreen mode Exit fullscreen mode

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:

# 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)]")
Enter fullscreen mode Exit fullscreen mode

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?

In the meantime, help grow this collection by hopping on my newsletter, visiting the shop, subscribing to my YouTube channel, and/or becoming a patron. In addition, you’re welcome to browse the following related articles:

Otherwise, thanks for stopping by! I appreciate the support.

The post 71 Python Code Snippets for Everyday Problems appeared first on The Renegade Coder.

Top comments (33)

Collapse
 
michelleanaya profile image
michelleanaya

I saved your article and python code. Maybe someday come in handy. I would also be interested to find information on python libraries.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Are there any libraries you’re interested in or would you want a general list?

Collapse
 
frosty110 profile image
Blaise D. Albuquerque

I think a general list of everyday libraries, what you use it for, and common methods would be interesting to see.

This is a great list, thank you! It's definitely super helpful for someone like me who's new to python.

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski

This is a great idea! I don't use a ton of libraries outside of what's already a part of the standard library (e.g. csv, sys, os, etc.), but I could probably piece together a similar article to this one.

Collapse
 
leahein profile image
Leah Einhorn

Nifty list!

Another way to clone a list:

my_duplicate_list = [*my_list]
Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Oh, this is cool! I can't believe there are even more slick ways to copy a list. Do you mind if I include this in the original article with credit to you for the tip?

Collapse
 
leahein profile image
Leah Einhorn

Absolutely, happy to contribute :)

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski

Just updated the article to include your solution. Did you know this solution is blazing fast?!

Collapse
 
richardcochrane profile image
Richard Cochrane

Super helpful list especially helping remind more experienced hands that your favourite approach is not necessarily the latest or preferred approach. Especially look forward to playing with f-strings when we do our Python 3 upgrade. Just one typo - in the last code block "import cProfile" is included in the comment rather than on it's own line.

Thanks again Jeremy!

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Thanks for the heads up, Richard! When I ported this over using RSS, the code blocks were imported as a single line, so I had to manually reformat them. I wouldn't be surprised if there were more artifacts like that.

Also, great call on the f-strings. I've been addicted to them for a little while now! Very convenient.

Collapse
 
bngcebetsha profile image
Buntu Ngcebetsha

where's the other like button so I can smash it many times :)

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Haha glad you like it. Unless this is a clever way of hinting at a dislike button. In that case, you win this time!

Collapse
 
bngcebetsha profile image
Buntu Ngcebetsha

Full proof reply, I've book marked the article and the reply for laughs later on in life...

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Very nice, covering a bunch of common problems Python programmers like myself face. I didn't know about all those quirks though.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

I’m a bit of a language feature nut! Glad you liked the list.

Collapse
 
hasii2011 profile image
Humberto A Sanchez II

Don't forget f-string formatting

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

I believe it’s there! Should be the last one in the string formatting list.

Collapse
 
yokotobe profile image
yokotobe

Helpful one

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Glad to hear it!

Collapse
 
iceorfiresite profile image
Ice or Fire

Awesome list @renegadecoder94

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Hope it’s helpful!

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Woah! That's a great list! 🤩

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Thanks for the kind words!

Collapse
 
ckesanapalli profile image
Chaitanya Kesanapalli

Thanks a lot for share these snippets.

In future can you also share snippets of logging, expection handling and main libraries like numpy arrays and pandas dataframes.

Collapse
 
zdev1official profile image
ZDev1Official

Bro that's good!
Solves %85 of my problems!

Collapse
 
sivanesh121 profile image
sivanesh121

super

Collapse
 
christianbos91 profile image
Christian Buendia

Awesome! Thanks for your time

Collapse
 
codeperfectplus profile image
Deepak Raj

Website to Learn Python, Machine Learning And Data Science.
bit.ly/codeperfectplus

Collapse
 
petermortensen profile image
Peter Mortensen

How did that add up to 71? I counted 16.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

71 refers to the number of code snippets (which could be wrong), not the number of problems.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.