When working with lists in Python, you often need to remove elements that exceed a certain threshold. Python offers several approaches to filter out elements larger than a specific value. Methods Overview The following methods can be used to remove elements larger than a specific value ? Using remove() Method (with caution) Using List Comprehension Using filter() Method with Lambda Function Using For Loop with append() Using remove() Method Warning: The remove() method can skip elements when modifying a list during iteration. Here's the problematic approach ? # This approach has ... Read More
In this article, we will learn how to remove leading zeros from a number given as a string using Python. Leading zeros are unnecessary zeros at the beginning of a number that don't affect its mathematical value. We'll explore three effective methods: using lstrip(), regular expressions, and the int() function. Each approach has its own advantages depending on your specific use case. Method 1: Using lstrip() Function The lstrip() method removes specified characters from the beginning of a string. This is the most straightforward approach for removing leading zeros ? def remove_leading_zeros(input_string): ... Read More
In this article, we will learn how to print a matrix in a snake pattern. A snake pattern prints the first row left to right, the second row right to left, the third row left to right, and so on, creating a zigzag or snake-like traversal. We will explore two different approaches to accomplish this task using Python. What is a Snake Pattern? Given a matrix, the snake pattern traversal means: Print even-indexed rows (0, 2, 4...) from left to right Print odd-indexed rows (1, 3, 5...) from right to left For example, a ... Read More
In this article, we will learn different methods to find the sum of a sublist in Python. A sublist is a contiguous portion of a list between specified start and end indices. Methods Used The following are the various methods to accomplish this task ? Using For Loop (Brute Force) Using Cumulative Sum Method Using sum() Function Using math.fsum() Function Using For Loop (Brute Force) This method iterates through the sublist elements and adds them one by one ? ... Read More
In this article, we will learn a Python Program to Convert Singular to Plural using various methods and libraries. Converting singular words to plural forms is a common task in natural language processing. While basic rules like adding 's' work for many words, English has complex pluralization rules that require more sophisticated approaches. Methods Used The following are the various methods to accomplish this task − Using the regex module Using NLTK and Pattern-en Packages Using the TextBlob module Using inflect module ... Read More
In this article, we will learn a Python program to check whether a matrix is an Involutory Matrix. An involutory matrix is a special type of square matrix that is its own inverse. What is an Involutory Matrix? An involutory matrix is a square matrix that, when multiplied by itself, gives the identity matrix. In mathematical terms, if A * A = I, then matrix A is called an involutory matrix, where I represents the Identity matrix. Matrix A: 1 ... Read More
In this article, we will learn a Python program to find the number of local extrema in an array. A local extrema is an element that is either a local maximum (greater than both neighbors) or a local minimum (less than both neighbors). The first and last elements cannot be extrema since they don't have two neighbors. What are Local Extrema? Consider an array element at position i. It is a local extrema if: Local Maximum: array[i] > array[i-1] and array[i] > array[i+1] Local Minimum: array[i] < array[i-1] and array[i] < array[i+1] ... Read More
Additive Secret Sharing and Share Proactivization are cryptographic techniques to share a password or other confidential data among a group of people. In this article, we will explain these techniques and implement Python code to demonstrate them. Additive Secret Sharing Additive secret sharing is a technique used in cryptography to share a secret string or a password among multiple parties, such that all of the parties must collaborate to reconstruct the secret. For example ? Original Secret Key: 1234 Generated five Shares: [-488, -55, -417, -720, 2914] Reconstructed Secret: 1234 Explanation: The original secret ... Read More
In this article, we will learn Matrix and linear Algebra calculations in Python such as matrix multiplication, finding determinants, solving linear equations, etc. A matrix object from the NumPy library can be used for this. When it comes to calculation, matrices are relatively comparable to the array objects. Linear Algebra is a huge topic, however, NumPy is an excellent library to start if you need to manipulate matrices and vectors. Operations Covered Finding Transpose of a Matrix Using Numpy Finding Inverse of a Matrix Using Numpy ... Read More
In this article, we will learn how to check if any set element exists in a list using three different Python methods. Methods Used Using any() function Using bitwise & operator Using Counter(), filter() and lambda functions Example Problem We have an input set and input list. We need to check whether any element from the set exists in the list ? Input inputSet = {4, 8, 1, 3, 5, 7} inputList = [7, 15, 20] Expected Output Checking whether any set element present in the input ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance