Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 805 of 855
Find indices with None values in given list in Python
Many times when dealing with data analysis we may come across None values present in a list. These values cannot be used directly in mathematical operations and string operations. So we need to find their position and either convert them or use them effectively. Using range() with List Comprehension Combining the range() and len() functions we can compare the value of each element with None and capture their index positions using list comprehension ? Example days = ['Sun', 'Mon', None, 'Wed', None, None] # Given list print("Given list :", days) # Using range ...
Read MoreFind fibonacci series upto n using lambda in Python
A Fibonacci series is a widely known mathematical sequence that explains many natural phenomena. It starts with 0 and 1, then each subsequent term is the sum of the two preceding terms. In this article, we will see how to generate a given number of Fibonacci terms using lambda functions in Python. Method 1: Using map() with Lambda We use the map() function to apply a lambda function that adds the sum of the last two terms to our list. The any() function is used to execute the map operation ? Example def fibonacci(count): ...
Read MoreFind elements within range in numpy in Python
Sometimes while processing data using the numpy library, we may need to filter out certain numbers in a specific range. This can be achieved by using some in-built methods available in numpy. Using logical_and() with where() In this approach we take a numpy array then apply the logical_and() function to it. The where() clause in numpy is also used to apply the and condition. The result is an array showing the position of the elements satisfying the required range conditions ? import numpy as np A = np.array([5, 9, 11, 4, 31, 27, 8]) ...
Read MoreFind depth of a dictionary in Python
A Python dictionary can be nested, meaning there are dictionaries within dictionaries. In this article, we will see how to calculate the level of nesting in a dictionary when there are nested dictionaries. Using String Conversion In this approach, we convert the entire dictionary into a string and count the number of opening braces { to determine the nesting level ? Example dictA = {1: 'Sun', 2: {3: {4: 'Mon'}}} dictStr = str(dictA) cnt = 0 for i in dictStr: if i == "{": ...
Read MoreFind common elements in three sorted arrays by dictionary intersection in Python
When working with Python data manipulation, you may need to find elements that are common among multiple arrays. This can be efficiently achieved by converting arrays into dictionaries using the Counter class from the collections module. The approach involves using Counter to count occurrences of each element, then finding the intersection using the & operator. This method preserves the minimum count of common elements across all arrays. Using Counter and Dictionary Intersection Here's how to find common elements using dictionary intersection ? from collections import Counter arrayA = ['Sun', 12, 14, 11, 34] arrayB ...
Read MoreFind all triplets in a list with given sum in Python
In a list of numbers we want to find out which three elements can join to give a certain sum. We call it a triplet. And in the list there can be many such triplets. For example, the sum 10 can be generated from numbers 1, 6, 3 as well as 1, 5, 4. In this article we will see how to find out all such triplets from a given list of numbers. Using Nested Loops with Set This approach uses nested loops with a set to efficiently find triplets. We iterate through the list, and for each ...
Read MoreFind all possible substrings after deleting k characters in Python
Sometimes we need to find all possible substrings after deleting exactly k characters from a string. This means keeping n-k characters in their original order to form new substrings. Python provides multiple approaches to solve this problem efficiently. Using Loops and Recursion This approach uses recursion to generate all combinations by selecting characters at different positions. We track the start and end positions while building each substring ? def letterCombinations(s, temp, start, end, index, k): result = [] if index == k: ...
Read MoreFind all distinct pairs with difference equal to k in Python
In this article, we will learn how to find all distinct pairs of numbers in a list where the absolute difference between the two numbers equals a given value k. We'll explore different approaches to solve this problem efficiently. Using Nested for Loops This approach uses two nested for loops to compare every possible pair of elements in the list. The outer loop visits each element, while the inner loop compares it with all remaining elements ? numbers = [5, 3, 7, 2, 9] k = 2 count = 0 # Check all possible pairs ...
Read MoreExtracting rows using Pandas .iloc[] in Python
Pandas is a powerful Python library extensively used for data processing and analysis. The .iloc[] method allows you to select rows and columns from a DataFrame using integer-based indexing, making it perfect for positional data extraction. The iloc method uses zero-based indexing where the first row has index 0, second row has index 1, and so on. Similarly, columns are indexed starting from 0. Syntax DataFrame.iloc[row_indexer, column_indexer] Creating Sample Data Let's create a sample DataFrame to demonstrate iloc functionality ? import pandas as pd # Create sample DataFrame data = ...
Read MoreDuplicate substring removal from list in Python
Sometimes we may have a need to refine a given list by eliminating duplicate substrings from delimited strings. This can be achieved by using a combination of various methods available in Python's standard library like set(), split(), and list comprehensions. Using set() and split() The split() method segregates the elements for duplicate checking, and the set() method stores only unique elements from each string. This approach maintains grouping by original string ? Example # initializing list strings = ['xy-xy', 'pq-qr', 'xp-xp-xp', 'dd-ee'] print("Given list:", strings) # using set() and split() result = [set(sub.split('-')) ...
Read More