Python Code Snippets

These Python code snippets do everything from creating singleton classes to merging dictionaries. Let’s enforce some Python best practices!

Add to Pieces

We've provided some common Python snippets that may be helpful in your development work. Feel free to add any of these Python code examples to your personal Pieces micro repository.

When downloading code snippets in Python to Pieces, developers will receive relevant tags for easy and fast search in Pieces, a description for context, and related links.

How to remove Python falsy values

Tags: python, remove falsy, list

Removes Python falsy values from a list by using the filter function.

def remove_falsy(unfiltered_list):
    return list(filter(bool, unfiltered_list)) 

Related links:

  1. Python code snippet to remove falsy values
  2. Remove falsy values from a list in Python

How to Flatten a List in Python

Tags: python, list, flatten, recursion

Flattens a nested list using recursion.

def flatten_list(nested_list):
    if not(bool(nested_list)):
        return nestedList
 
    if isinstance(nested_list[0], list):
        return flatten_list(*nested_list[:1]) + flatten_list(nested_list[1:])
 
    return nested_list[:1] + flatten_list(nested_list[1:]) 

Related links:

  1. Python Program to Flatten a nested List using Recursion
  2. Flatten a nested list in Python

How to check for duplicates in a list Python code

Tags: python, list, check duplicates

Using this Python code checks whether a list has duplicate values by using set to grab only unique elements.

def check_for_duplicates(input):
    return len(input) != len(set(input))

Related links:

  1. Python Sets
  2. Set types in Python

How to check memory usage

Tags: python, memory usage, memory

This snippet in Python checks the total memory an object consumes.

import sys

a = 100;
print(sys.getsizeof(a)) 

Related links:

  1. How to check memory usage in Python
  2. How much of your memory is used by an object in Python
  3. Use Python to check memory size of an object

How to sort a list of dictionaries in python

Tags: python, dictionaries

Sorts a list of dictionaries in Python.

new_dict = {k: v for k, v in sorted(d.items(), key=lambda item: item[1])} 

Related links:

  1. Sort Python dictionaries by key of value
  2. Sort dictionary by value in Python

How to merge multiple dictionaries in Python

Tags: python, merge dictionary, dictionary

Merge two python dictionaries into one, or as many dictionaries as you have.

def merge_dicts(*dicts):
  super_dict = {}
  for dict in dicts:
      for k, v in dict.items():
          super_dict[k] = v

 return super_dict

Related links:

  1. How to merge dictionaries in Python
  2. 5 ways to merge dictionaries in Python

How to check if a file exists in Python

Tags: python, file

Checks if a file exists in Python or not.

from os import path

def check_for_file(file_name):
	print("File exists: ", path.exists(file_name))

Related links:

  1. How to check if a file exists or not in Python
  2. How to check if a file already exists in Python

How to use Python to merge list of lists

Tags: python, file

Merge a series of lists into a list of lists. For example, [1, 2, 3] and [4, 5, 6] would merge into [[1, 2, 3], [4, 5, 6]].

def merge(*args, missing_val = None):
  max_length = max([len(lst) for lst in args])
  out_list = []

  for i in range(max_length):
    out_list.append([args[k][i] if i < len(args[k]) else missing_val for k in range(len(args))])

  return out_list 

Related links:

  1. Python Snippets you need to know
  2. Args and Kwargs in Python

How to parse data from a CSV file in Python

Tags: python, csv

Use this Python snippet to parse CSV data and store it in a list of dictionaries.

import csv

def parse_csv_data(csv_path):
	csv_mapping_list = []

	with open(csv_path) 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 

Related links:

  1. Parse CSV data in Python
  2. How to Parse a CSV File in Python

List Comprehension with Conditionals

Tags: python, list comprehension

Perform different operations in a list comprehension depending on a conditional.

l = [-1, 3, -4, 5, 6, -9]
l = [x if x >= 0 else 0 for x in l] 

Related links:

  1. If else in a list comprehension
  2. List comprehensions in Python
  3. When to use list comprehensions in Python

How to write JSON data to file in Python

Tags: python, json, json write

Write data to a JSON file using python.

import json

def write_json_to_file(data, filepath):
	with open(filename, "w") as f:
		json.dump(data, f, indent=4) 

Related links:

  1. How do I write JSON data to a file
  2. Use Python to write JSON to a file

How to read data from a JSON file in Python

Tags: python, json, json read

Read JSON data from a file.

import json

def read_json_from_file(filepath):
	with open(filepath, "r") as f:
    data = json.load(f) 

Related links:

  1. Read JSON file using Python
  2. Python JSON read write examples

How to create abstract base class in Python

Tags: python, abstract base class, abc

Creates an abstract base class to test if objects adhere to given specifications.

from abc import ABCMeta, abstractmethod


class BaseClass(metaclass=ABCMeta):
    @abstractmethod
    def foo(self):
        pass

    @abstractmethod
    def bar(self):
        pass


class ConcreteClass(BaseClass):
    def foo(self):
        pass

    def bar(self):
        pass


instance = ConcreteClass() 

Related links:

  1. Abstract base classes
  2. Abstract base class Python examples

Creating a thread pool in python

Tags: python, thread pool, threads

Creates a thread pool.

import threading
import time
from queue import Queue


def f(n):
    time.sleep(n)


class Worker(threading.Thread):
    def __init__(self, queue):
        super(Worker, self).__init__()
        self.q = queue
        self.daemon = True
        self.start()

    def run(self):
        while 1:
            f, args, kwargs = self.q.get()
            try:
                f(*args, **kwargs)
            except Exception as e:
                print(e)
            self.q.task_done()


class ThreadPool(object):
    def __init__(self, thread_num=10):
        self.q = Queue(thread_num)
        for i in range(thread_num):
            Worker(self.q)

    def add_task(self, f, *args, **kwargs):
        self.q.put((f, args, kwargs))

    def wait_complete(self):
        self.q.join()


if __name__ == '__main__':
    start = time.time()
    pool = ThreadPool(5)
    for i in range(10):
        pool.add_task(f, 3)
    pool.wait_complete()
    end = time.time() 

Related links:

  1. Concurrency in Python - thread pools
  2. Python thread pool executor example

How to set up a Python virtual environment

Tags: python, virtual environment

Create a virtual environment in Python to manage dependencies.

python -m venv projectnamevenv 

Related links:

  1. How to create a virtual environment in Python
  2. Python virtual environments a primer
  3. Why to use virtual environments in Python

Python Singleton decorator

Tags: python, virtual environment, singleton decorator, python singleton

Decorator to create a singleton class.

def singleton(myClass):
	instances = {}
	def getInstance(*args, **kwargs):
		if myClass not in instances:
			instances[myClass] = myClass(*args, **kwargs)
		return instances[myClass]
	return getInstance

@singleton
class TestClass(object):
	pass 

Related links:

  1. What is a singleton pattern in Python
  2. Singleton design pattern in Python

Creating your own data stream in Python

Tags: python, stream

This Python code snippet example creates a stream.

def processor(reader, converter, writer):
    while True:
        data = reader.read()
        if not data:
            break
        data = converter(data)
        writer.write(data)


class Processor:
    def __init__(self, reader, writer):
        self.reader = reader
        self.writer = writer

    def process(self):
        while True:
            data = self.reader.readline()
            if not data:
                break
            data = self.converter(data)
            self.writer.write(data)

    def converter(data):
        assert False, 'converter must be defined' 

Related links:

  1. Data streams in Python
  2. Python concurrency - Streams

How to do logging in Python

Tags: python, logging

Set logging for debug level along with file name and output to a sample.log file.

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')

file_handler = logging.FileHandler('sample.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(stream_handler) 

Related links:

  1. How to use logging in Python
  2. Logging in Python

Want to use these Python code snippets in your IDE? Download our JetBrains plugin or VS Code extension to improve your developer productivity wherever you code.

Let us know what you think! Would you like to see other useful Python snippets not listed on this page? Suggest a collection you'd like to see to help other developers speed up their workflows.