Suppose you’re new to data science or programming. You probably often hear people recommending Python as the first programming language to learn because it has a simple-to-understand syntax and better code readability. Together, these features make Python much easier to learn for people with no prior programming experience.
Python is one of the most widely used programming languages today with applications in various fields including data science, scientific computing, web dev, game dev and building desktop graphical interfaces. Python gained this popularity for its usefulness in various fields, how productive it is compared to other programming languages like Java, C, and C++, and how English-like its commands are.
When you use Python every day to solve challenges, develop algorithms and build applications, you’ll find yourself repeating some tasks over and over again. That’s why it’s a good idea to have some code snippets ready for commonly performed tasks.
There are many snippets out there for Python categorized by field but, for this article, I’ll focus on general-purpose snippets you can use for any application.
13 Python Snippets For Your Toolkit
- Merge two lists into a dictionary
- Merge two or more lists into a list of lists
- Sort a list of dictionaries
- Sort a list of strings
- Sort a list based on another list
- Map a list into a dictionary
- Merging two or more dictionaries
- Inverting a dictionary
- Using f strings
- Checking for substrings
- Get a string’s size in bytes
- Checking if a file exists
- Parsing a spreadsheet
List Snippets
We’ll start with a few snippets about Python’s most-used data structure: lists.
1. Merge Two Lists Into a Dictionary
Assume we have two lists in Python and we want to merge them in a dictionary form, where one list’s items act as the dictionary’s keys and the other’s as the values. This is a frequent problem often faced when writing code in Python.
To solve this problem, we need to consider a couple of restrictions, such as the sizes of the two lists, the types of items in the two lists and if there are any repeated items in them, especially in the one we’ll use as keys. We can overcome that with the use of built-in functions like zip.
keys_list = ['A', 'B', 'C']
values_list = ['blue', 'red', 'bold']
#There are 3 ways to convert these two lists into a dictionary
#1- Using Python's zip, dict functionz
dict_method_1 = dict(zip(keys_list, values_list))
#2- Using the zip function with dictionary comprehensions
dict_method_2 = {key:value for key, value in zip(keys_list, values_list)}
#3- Using the zip function with a loop
items_tuples = zip(keys_list, values_list)
dict_method_3 = {}
for key, value in items_tuples:
if key in dict_method_3:
pass # To avoid repeating keys.
else:
dict_method_3[key] = value
2. Merge Two or More Lists Into a List of Lists
Another frequent task is when we have two or more lists, and we want to collect them all in one big list of lists, where all the first items of the smaller list form the first list in the bigger list.
For example, if I have 4 lists [1,2,3]
, [‘a’,’b’,’c’]
, [‘h’,’e’,’y’]
and [4,5,6]
and we want to make a new list of those four lists; it will be [[1,’a’,’h’,4]
, [2,’b’,’e’,5]
, [3,’c’,’y’,6]]
.
def merge(*args, missing_val = None):
#missing_val will be used when one of the smaller lists is shorter tham the others.
#Get the maximum length within the smaller lists.
max_length = max([len(lst) for lst in args])
outList = []
for i in range(max_length):
result.append([args[k][i] if i < len(args[k]) else missing_val for k in range(len(args))])
return outList
3. Sort a List of Dictionaries
The next set of everyday list tasks is sorting. Depending on the data type of the items included in the lists, we’ll follow a slightly different way to sort them. Let’s first start with sorting a list of dictionaries.
dicts_lists = [
{
"Name": "James",
"Age": 20,
},
{
"Name": "May",
"Age": 14,
},
{
"Name": "Katy",
"Age": 23,
}
]
#There are different ways to sort that list
#1- Using the sort/ sorted function based on the age
dicts_lists.sort(key=lambda item: item.get("Age"))
#2- Using itemgetter module based on name
from operator import itemgetter
f = itemgetter('Name')
dicts_lists.sort(key=f)
4. Sort a List of Strings
We’re often faced with lists containing strings, and we need to sort those lists alphabetically, by length or any other factor we want (or that our application needs).
Now, I should mention that these are straightforward ways to sort a list of strings, but you may sometimes need to implement your sorting algorithm to solve that problem.
my_list = ["blue", "red", "green"]
#1- Using sort or srted directly or with specifc keys
my_list.sort() #sorts alphabetically or in an ascending order for numeric data
my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest.
# You can use reverse=True to flip the order
#2- Using locale and functools
import locale
from functools import cmp_to_key
my_list = sorted(my_list, key=cmp_to_key(locale.strcoll))
5. Sort a List Based on Another List
Sometimes, we may want or need to use one list to sort another. So, we’ll have a list of numbers (the indexes) and a list that I want to sort using these indexes.
a = ['blue', 'green', 'orange', 'purple', 'yellow']
b = [3, 2, 5, 4, 1]
#Use list comprehensions to sort these lists
sortedList = [val for (_, val) in sorted(zip(b, a), key=lambda x: \
x[0])]
6. Map a List Into a Dictionary
The last list-related task we’ll look at in this article is if we’re given a list and map it into a dictionary. That is, I want to convert my list into a dictionary with numerical keys.
mylist = ['blue', 'orange', 'green']
#Map the list into a dict using the map, zip and dict functions
mapped_dict = dict(zip(itr, map(fn, itr)))
Dictionary Snippets
The next data type we will address is dictionaries.
7. Merging Two or More Dictionaries
Assume we have two or more dictionaries, and we want to merge them all into one dictionary with unique keys. Here’s what that will look like:
from collections import defaultdict
#merge two or more dicts using the collections module
def merge_dicts(*dicts):
mdict = defaultdict(list)
for d in dicts:
for key in d:
mdict[key].append(d[key])
return dict(mdict)
8. Inverting a Dictionary
One common dictionary task is flipping a dictionary’s keys and values. So, the keys will become the values, and the values will become the keys.
When we do that, we need to make sure we don’t have duplicate keys. While values can be repeated, keys cannot. Also make sure all the new keys are hashable.
my_dict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
#Invert the dictionary based on its content
#1- If we know all values are unique.
my_inverted_dict = dict(map(reversed, my_dict.items()))
#2- If non-unique values exist
from collections import defaultdict
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}
#3- If any of the values are not hashable
my_dict = {value: key for key in my_inverted_dict for value in my_inverted_dict[key]}
String Snippets
9. Using F Strings
Formatting a string is probably the number one task you’ll need to do almost daily. There are various ways you can format strings in Python; my favorite one is using f strings.
#Formatting strings with f string.
str_val = 'books'
num_val = 15
print(f'{num_val} {str_val}') # 15 books
print(f'{num_val % 2 = }') # 1
print(f'{str_val!r}') # books
#Dealing with floats
price_val = 5.18362
print(f'{price_val:.2f}') # 5.18
#Formatting dates
from datetime import datetime;
date_val = datetime.utcnow()
print(f'{date_val=:%Y-%m-%d}') # date_val=2021-09-24
10. Checking for Substrings
One of the most common tasks I’ve needed to perform is to check if a string is in a list of strings.
addresses = ["123 Elm Street", "531 Oak Street", "678 Maple Street"]
street = "Elm Street"
#The top 2 methods to check if street in any of the items in the addresses list
#1- Using the find method
for address in addresses:
if address.find(street) >= 0:
print(address)
#2- Using the "in" keyword
for address in addresses:
if street in address:
print(address)
11. Get a String’s Size in Bytes
Sometimes, especially when building memory-critical applications, we need to know how much memory our strings are using. Luckily, we can do this quickly with one line of code.
str1 = "hello"
str2 = "?"
def str_size(s):
return len(s.encode('utf-8'))
str_size(str1)
str_size(str2)
Input/Output Operations
12. Checking if a File Exists
Data scientists often need to read data from files or write data to them. To do that, we need to check if a file exists or not so our code doesn’t terminate with an error.
#Checking if a file exists in two ways
#1- Using the OS module
import os
exists = os.path.isfile('/path/to/file')
#2- Use the pathlib module for a better performance
from pathlib import Path
config = Path('/path/to/file')
if config.is_file():
pass
13. Parsing a Spreadsheet
Another common file interaction is parsing data from a spreadsheet. Luckily, we have the CSV module to help us perform that task efficiently.
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
Takeaways
As a computer science instructor, I meet people from different age groups that want to learn to program. They usually come to me with a programming language in mind or an application field they want to get into. My younger students often just want to learn to code, while older students want to get into a specific area like data science or web development.
When I teach programming, I like to make things simple. I’ve found that if I teach students the basics and help them with the building blocks, they’ll be able to develop more significant projects using those foundational tools. That’s when I started collecting useful Python code snippets to share with my students and use in my own work.
Now you can add these 13 code snippets to your own snippets database (or start one!). These snippets are simple, short and efficient. You’ll end up using at least one of them in any Python project regardless of which application field you’re working in. Have fun!