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 … 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 … Read more

Python One-Liner Webserver HTTP

Want to create your own webserver in a single line of Python code? No problem, just use this command in your shell: The terminal will tell you: To shut down your webserver, kill the Python program with CTRL+c. This works if you’ve Python 3 installed on your system. To check your version, use the command … 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 … 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 … Read more