Month: June 2020

What Does “if __name__ == ‘__main__'” Do in Python?

Today, let’s discuss something that’s all over the place in many code bases: what does if __name__ == ‘__main__’ do in Python? The statement if __name__ == ‘__main__’: checks if variable __name__ is set to the string value ‘__main__’ which holds only within the main source file from which you initially execute your code. In all other …

What Does “if __name__ == ‘__main__’” Do in Python? Read More »

How to Get Specific Elements From a List? – Most Pythonic Way!

Quick Article Summary to get a specific element from a list: Get elements by index use the operator [] with the element’s index use the list’s method pop(index) use slicing lst[start:stop:step] to get several elements at once use the function itemgetter() from the operator module Get elements by condition use the filter() function use a …

How to Get Specific Elements From a List? – Most Pythonic Way! Read More »

Dict to List — How to Convert a Dictionary to a List in Python

Summary: To convert a dictionary to a list of tuples, use the dict.items() method to obtain an iterable of (key, value) pairs and convert it to a list using the list(…) constructor: list(dict.items()). To modify each key value pair before storing it in the list, you can use the list comprehension statement [(k’, v’) for …

Dict to List — How to Convert a Dictionary to a List in Python 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 »

Python Freelancing | How to Exploit This Disruptive Mega Trend (as a Coder)

Short summary of the main points in the video: Freelancing is a mega-trend that will disrupt the organization of the world’s labor in the next 10-20 years. Freelancing platforms such as Upwork and Fiverr grow at 20% per year. You can participate in this trend by focusing on one tiny niche—and become world-class at it. …

Python Freelancing | How to Exploit This Disruptive Mega Trend (as a Coder) 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 Two Ordered Lists Are Identical

The most Pythonic way to check if two ordered lists l1 and l2 are identical, is to use the l1 == l2 operator for element-wise comparison. If all elements are equal and the length of the lists are the same, the return value is True. Problem: Given are two lists l1 and l2. You want …

The Most Pythonic Way to Check if Two Ordered Lists Are Identical Read More »

Convert Tuple to List | The Most Pythonic Way

Answer: The simplest, most straightforward, and most readable way to convert a tuple to a list is Python’s built-in list(tuple) function. You can pass any iterable (such as a tuple, another list, or a set) as an argument into this so-called constructor function and it returns a new list data structure that contains all elements …

Convert Tuple to List | The Most Pythonic Way Read More »

How to Test Multiple Variables Against a Value in Python?

To test multiple variables x, y, z against a value in Python, use the expression value in {x, y, z}. Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value. Here’s the code: Exercise: Add a fourth variable and run the modified …

How to Test Multiple Variables Against a Value in Python? Read More »