Python Set

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 »

How to Solve Python “TypeError: β€˜int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: β€˜int’ object is not iterable”? Read More »

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with …

How To Split A String And Keep The Separators? Read More »

Python One Line Append

Do you want to one-linerize the append() method in Python? I feel you—writing short and concise one-liners can be an addiction! πŸ™‚ This article will teach you all the ways to append one or more elements to a list in a single line of Python code! Python List Append Let’s quickly recap the append method …

Python One Line Append Read More »

The World’s Most Concise Python Cheat Sheet

Do you want to learn Python but you’re overwhelmed and you don’t know where to start? Learn with Python cheat sheets! They compress the most important information in an easy-to-digest 1-page format. Here’s the new Python cheat sheet I just created—my goal was to make it the world’s most concise Python cheat sheet!

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 Compare Two Lists in Python

Problem: Given are two lists l1 and l2. You want to perform either of the following: 1. Boolean Comparison: Compare the lists element-wise and return True if your comparison metric returns True for all pairs of elements, and otherwise False. 2. Difference: Find the difference of elements in the first list but not in the …

The Most Pythonic Way to Compare Two Lists in Python Read More »

List Difference | The Most Pythonic Way

Short answer: The most Pythonic way to compute the difference between two lists l1 and l2 is the list comprehension statement [x for x in l1 if x not in set(l2)]. This works even if you have duplicate list entries, it maintains the original list ordering, and it’s efficient due to the constant runtime complexity …

List Difference | The Most Pythonic Way Read More »

The Most Pythonic Way to Check if Two Unordered Lists Are Identical

To check if two unordered lists x and y are identical, compare the converted sets with set(x) == set(y). However, this loses all information about duplicated elements. To consider duplicates, compare the sorted lists with sorted(x) == sorted(y). Due to the efficient merge-sort-like implementation of the sorted() function, this is quite fast for almost-sorted lists. …

The Most Pythonic Way to Check if Two Unordered Lists Are Identical 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 »

string.join(list) vs list.join(string) | Why Python’s Creators Chose The Former

If you’re like me, you may have asked yourself the following question: Why is it string.join(list) instead of list.join(string) in Python? πŸ™„ The join method works with any iterable, not just with lists. Therefore, you’d have to implement it in hundreds of classes instead of just one (in the string class). Additionally, join() works only …

string.join(list) vs list.join(string) | Why Python’s Creators Chose The Former Read More »

How to Combine Two Python Lists and Remove Duplicates in Second List?

Problem: Given two lists [1, 2, 2, 4] and [2, 5, 5, 5, 6]. How do you combine those lists to the new list [1, 2, 2, 4, 5, 6] by removing the duplicates in the second list? Note: You want to remove all duplicates in the second list and the elements in the second …

How to Combine Two Python Lists and Remove Duplicates in Second List? Read More »