Python One-Liners

Python Join List with Underscore [The Most Pythonic Way]

The ‘_’.join(list) method on the underscore string ‘_’ glues together all strings in the list using the underscore string as a delimiter—and returns a new string. For example, the expression ‘_’.join([‘a’, ‘b’, ‘c’]) returns the new string ‘a_b_c’. Definition and Usage The string.join(iterable) method joins the string elements in the iterable to a new string …

Python Join List with Underscore [The Most Pythonic Way] Read More »

Check Python Version in Your Script — A Helpful Illustrated Guide

To check your version in your Python script—for example, to make sure that a user of your Python app uses the correct version—you can use the following commands. To achieve this, simply import the sys module and print the sys.version attribute to your Python shell: Try them in our interactive browser-based Python shell: Exercise: execute …

Check Python Version in Your Script — A Helpful Illustrated Guide Read More »

How to Check Your Python Version? Run This in Your Script or {Mac/Linux/Win} Shell

Abstract: To check your Python version, run python –version in your command line (Windows), shell (Mac), or terminal (Linux/Ubuntu). To check your Python version in your script, run import sys to get the module and use sys.version to find detailed version information in your code. When Guido van Rossum released the first viable Python version …

How to Check Your Python Version? Run This in Your Script or {Mac/Linux/Win} Shell Read More »

Top 18 Cool Python Tricks

What are the coolest Python tricks? I’ve compiled this list of the best Python tricks—in reverse order. Without further ado, let’s dive into those crazy one-liner Python features, tricks, and functions: 18. Modifying Iterable Elements 1/2 The function map(func, iter) executes the function func on all elements of the iterable iter. Related article: Which is …

Top 18 Cool Python Tricks Read More »

How to Add an Element to a Python List at an Index?

To add an element to a given Python list, you can use either of the three following methods: Use the list insert method list.insert(index, element). Use slice assignment lst[index:index] = [element] to overwrite the empty slice with a list of one element. Use list concatenation with slicing lst[:2] + [‘Alice’] + lst[2:] to create a …

How to Add an Element to a Python List at an Index? Read More »

How to Find the Minimum of a List of Lists in Python?

Short answer: To find the minimal list in a list of lists, you need to make two lists comparable. How? With the key argument of the min() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the smallest numerical value is …

How to Find the Minimum of a List of Lists in Python? Read More »

How to Find the Max of a List of Lists in Python?

Short answer: To find the maximal list in a list of lists, you need to make two lists comparable. How? With the key argument of the max() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the largest numerical value is …

How to Find the Max of a List of Lists in Python? Read More »

How to Filter a List of Lists in Python?

Short answer: To filter a list of lists for a condition on the inner lists, use the list comprehension statement [x for x in list if condition(x)] and replace condition(x) with your filtering condition that returns True to include inner list x, and False otherwise. Lists belong to the most important data structures in Python—every …

How to Filter a List of Lists in Python? Read More »

How to Sum List of Lists in Python? [Rows]

Problem: Given a list of lists representing a data matrix with n rows and m columns. How to sum over the rows of this matrix? In this article, you’re going to learn different ways to accomplish this in Python. Let’s ensure that you’re on the same page. Here’s a graphical representation of the list of …

How to Sum List of Lists in Python? [Rows] Read More »