Regex

How to Search and Replace a Line in a File in Python?

Problem: Given the contents of a text file. How to search and replace a specific string or line in the file? Example: Let’s consider the following example. Text in the file before replacing a line: There was an idea to bring together a group of remarkable people to see if we could become something more. …

How to Search and Replace a Line in a File in Python? Read More »

Python One Line X

This is a running document in which I’ll answer all questions regarding the single line of Python code. If you want to become a one-liner wizard, check out my book “Python One-Liners”! ๐Ÿ™‚ This document contains many interactive code shells and videos to help you with your understanding. However, it’s pretty slow because of all …

Python One Line X Read More »

The Most Pythonic Way to Check If a List Contains an Element

The standard way of checking if an element exists in a list is to use the in keyword. For example, ‘Alice’ in [1, ‘Alice’, 3] will return True while the same returns False for ‘Bob’. If you need to check more complicated conditions, use the any(condition(x) for x in list) function with a generator expression. …

The Most Pythonic Way to Check If a List Contains an Element Read More »

Does the Dot Regex (.) Match Whitespace Characters in Python?

Yes, the dot regex matches whitespace characters when using Python’s re module. Consider the following example: Try it yourself in our interactive Python shell (click “run”): The dot matches all characters in the string–including whitespaces. You can see that there are many whitespace characters ‘ ‘ among the matched characters. Need more info? Watch the …

Does the Dot Regex (.) Match Whitespace Characters in Python? Read More »

The Most Pythonic Way to Check if a Python String Contains Another String? (Tutorial + Video)

How to check if a Python string s1 contains another string s2? There are two easy ways to check whether string s1 contains string s2: Use the expression s2 in s1, or Use the find method s1.find(s2). You can try both methods in our interactive Python shell (just click “Run” to execute the code in …

The Most Pythonic Way to Check if a Python String Contains Another String? (Tutorial + Video) Read More »

Python Lists filter() vs List Comprehension – Which is Faster?

[Spoiler] Which function filters a list faster: filter() vs list comprehension? For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method. To answer this question, I’ve written a short script that tests the runtime performance of filtering large lists of increasing sizes using the filter() …

Python Lists filter() vs List Comprehension – Which is Faster? Read More »

How to Match an Exact Word in Python Regex? (Answer: Don’t)

This morning, I read over an actual Quora thread with this precise question. While there’s no dumb question, the question reveals that there may be some gap in understanding the basics in Python and Python’s regular expression library. So if you’re an impatient person, here’s the short answer: How to match an exact word/string using …

How to Match an Exact Word in Python Regex? (Answer: Don’t) Read More »

What are Regular Expressions Used For? 10 Applications

The web is full of tutorials about regular expressions. But I realized that most of those tutorials lack a thorough motivation. Why do regular expressions exist? What are they used for? What are some practical applications? Somehow, the writers of those tutorials believe that readers are motivated by default to learn a technology that’s complicated …

What are Regular Expressions Used For? 10 Applications Read More »