Python Snippets

The Python snippet collection contains helper functions for Python 3.6. It includes utilities for most common data types, such as primitivies, lists, dictionaries and date objects.

  • Delayed function execution

    Invokes the provided function after ms milliseconds.

    Python, Function · Nov 2, 2020

  • How to correctly close files in Python

    When working with files in Python, it's important to ensure that the file is closed correctly. Here are a couple of ways to do that.

    Python, File · Feb 3, 2022

  • Check for duplicates in list

    Checks if there are duplicate values in a flat list.

    Python, List · Nov 2, 2020

  • List head

    Returns the head of a list.

    Python, List · Sep 15, 2020

  • List without last element

    Returns all the elements of a list except the last one.

    Python, List · Nov 2, 2020

  • List intersection

    Returns a list of elements that exist in both lists.

    Python, List · Nov 2, 2020

  • List is contained in other list

    Checks if the elements of the first list are contained in the second one regardless of order.

    Python, List · Jan 7, 2021

  • Last list element

    Returns the last element in a list.

    Python, List · Nov 2, 2020

  • Palindrome

    Checks if the given string is a palindrome.

    Python, String · Nov 2, 2020

  • How do I trim whitespace from a string in Python?

    Oftentimes you might need to trim whitespace from a string in Python. Learn of three different way to do this in this short guide.

    Python, String · Dec 13, 2021

  • List union

    Returns every element that exists in any of the two lists once.

    Python, List · Nov 2, 2020

  • Add days to date

    Calculates the date of n days from the given date.

    Python, Date · Oct 28, 2020

  • Clamp number

    Clamps num within the inclusive range specified by the boundary values.

    Python, Math · Nov 2, 2020

  • Number in range

    Checks if the given number falls within the given range.

    Python, Math · Sep 15, 2020

  • Number is prime

    Checks if the provided integer is a prime number.

    Python, Math · Nov 2, 2020