Data Structures

The Fastest Python Method To Compute All Primes < N

Overview Computing primes from a given series of numbers might not be a big issue. However, computing primes in the most effective manner keeping the time complexity and other factors in mind might be a challenging issue. After all, one of the major criteria of an effective code is its efficiency. Therefore, in this article, …

The Fastest Python Method To Compute All Primes < N Read More »

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 »

The Mutable Default Argument In Python

Summary: Passing mutable objects as default arguments leads to unexpected outputs because Python initializes the default mutable object only once, not (as you may have expected) each time the function is called. To fix this, initialize the mutable default argument with the None keyword in the argument list and then initialize it within the function. …

The Mutable Default Argument In Python Read More »

Slice Notation – A Simple Illustrated Guide

Summary: Slicing is a Python concept to extract a subsequence from a string or list—that lies within a start and stop index range. There are two syntactical ways to define a slice. (1) The extended slice notation makes use of a colon : in string_name[start:stop:step]. (2) The slice() constructor defines the index range in string_name[slice(start:stop:step)]. …

Slice Notation – A Simple Illustrated Guide 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 »

How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist?

Summary: To update a key in a dictionary if it doesn’t exist, you can check if it is present in the dictionary using the in keyword along with the if statement and then update the key-value pair using subscript notation or update() method or the * operator. Another workaround for this is, using the setdefault(key[, default]) …

How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? Read More »

List Changes After Assignment — How to Clone or Copy It?

Problem: If you assign a list object to a new variable using new_list = old_list, any modification to new_list changes old_list. What’s the reason for this and how can you clone or copy the list to prevent this problem? Example: Let’s consider the following example. Appending an element to the new_list also modifies the original …

List Changes After Assignment — How to Clone or Copy It? Read More »

How To Format A String That Contains Curly Braces In Python?

Summary: Use one of the following methods to format strings that contain curly braces: Use double curly braces {{}} Use the old string formatting, i.e. the % operator Use the JSON Library Use Template Strings Problem: Given a string literal with curly braces; how to format the string and ensure that the curly braces are …

How To Format A String That Contains Curly Braces 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 »