Python One-Liners

How To Eliminate All The Whitespace From A String?

In this article, you’ll learn the ultimate answer to the following question: How To Eliminate All The Whitespace From A String—on Both Ends, and In-Between Words? Summary: Use the string methods join(), split(), strip(), rstrip(), lstrip() and or replace()—in specific combinations—to remove any whitespace in a given string. The simplest way to remove all whitespaces …

How To Eliminate All The Whitespace From A String? Read More »

Python Reverse List with Slicing — An Illustrated Guide

Summary: The slice notation list[::-1] with default start and stop indices and negative step size -1 reverses a given list. Problem: Given a list of elements. How to reverse the order of the elements in the list. Example: Say, you’ve got the following list: Your goal is to reverse the elements to obtain the following …

Python Reverse List with Slicing — An Illustrated Guide Read More »

How to Remove Duplicates From a Python List While Preserving Order?

To remove duplicates from a Python list while preserving the order of the elements, use the code list(dict.fromkeys(list)) that goes through two phases: (1) Convert the list to a dict using the dict.fromkeys() function with the list elements as keys and None as dict values. (2) Convert the dictionary back to a list using the …

How to Remove Duplicates From a Python List While Preserving Order? Read More »

How to Get the Last Element of a Python List?

Problem: Given a list. How to access the last element of this list? Example: You have the list [‘Alice’, ‘Bob’, ‘Liz’] and you want to get the last element ‘Liz’. Quick solution: Use negative indexing -1. To access the last element of a Python list, use the indexing notation list[-1] with negative index -1 which …

How to Get the Last Element of a Python List? Read More »

56 Python One-Liners to Impress Your Friends

This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells. Let’s get started! Python One Line If Else You can use a simple if statement in a single line of code. …

56 Python One-Liners to Impress Your Friends Read More »

Accessing The Index Of Iterables In Python

Summary: To access the index of iterables like lists in Python, use one of the following methods: Use enumerate() function. Use a counter variable with For Loop/While Loop. Use a list comprehension. Use the NumPy library. Use the itertools module. Introduction An index can be considered as the position of an element in an ordered …

Accessing The Index Of Iterables In Python Read More »

Python One Line Dictionary

Python’s dictionary data structure is one of the most powerful, most underutilized data structures in Python. Why? Because checking membership is more efficient for dictionaries than for lists, while accessing elements is easier for dictionaries than for sets. In this tutorial, you’ll learn how to perform four common dictionary operations in one line of Python …

Python One Line Dictionary Read More »

Python One-Line Password Generator

Can you believe it? People use unknown and potentially insecure websites to generate their random passwords! This works as follows: A website generates a “random” password for them and they copy&paste it and assume this is a safe password because of the randomness of the characters. What a security flaw! Why? Because the website could …

Python One-Line Password Generator Read More »

Regex to Match Dollar Amounts with Optional Cents

Problem: How to match dollar amounts in a given string? If cent amounts are given, those should match as well. Solution: You can use regular expression pattern ‘(\$[0-9]+(.[0-9]+)?)’ to find all numbers that start with a dollar symbol, followed by an arbitrary number of numerical digits, followed by an optional decimal point and an arbitrary …

Regex to Match Dollar Amounts with Optional Cents Read More »

Python Global in One Line

To update a global variable in one line of Python, retrieve the global variable dictionary with the globals() function, and access the variable by passing the variable name as a string key such as globals()[‘variable’]. Then overwrite the global variable using the equal symbol, for example in globals()[‘variable’] = 42 to overwrite the variable with …

Python Global in One Line Read More »

Python Comments — 2-Minute Guide with Exercise

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term! There are two types of comments: one-line comments and multi-line …

Python Comments — 2-Minute Guide with Exercise Read More »