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
Programming Articles
Page 39 of 2547
Python Program to count the number of lists in a list of lists
Python provides several approaches to count the number of lists contained within a list of lists. This is useful when working with nested data structures where you need to determine how many sublists exist. In this article, we'll explore three different methods to count lists in a list of lists using Python. Using Iterative Approach The iterative approach uses a simple loop to count lists by checking each element's type ? Algorithm Step 1 − Create a function that takes the list of lists as a parameter. Step 2 − Initialize a counter variable to ...
Read MoreAltering duplicate values from a given Python list
Working with data in Python frequently involves handling lists, which are fundamental data structures. However, managing duplicate values within a list can present challenges. While removing duplicates is a common task, there are circumstances where altering duplicate values and preserving the overall structure of the list becomes necessary. In this article, we'll explore different approaches to handle this specific issue. Instead of removing duplicate values, we'll focus on modifying them. Modifying duplicate values can be valuable in various scenarios, such as distinguishing between unique and duplicate entries or tracking the frequency of duplicates. Why Alter Duplicate Values? ...
Read MorePython – Check Element for Range Occurrences
In Python, checking if an element exists within a specific range of values is a common programming task. This can be accomplished using various approaches, from simple iteration to more advanced search algorithms. What is Range Occurrence Checking? Range occurrence checking involves determining whether a specific element exists within a collection of values (like a dictionary, list, or range). The "range" here refers to a bounded set of elements with defined start and end points. Method 1: Using Flag-Based Iteration This approach uses a boolean flag to track whether the target element is found during iteration ...
Read MoreRemove all strings from a list of tuples in Python
When working with a list of tuples in Python, you may need to remove all string elements from each tuple while keeping non-string elements. This article explores three different approaches to accomplish this task using list comprehension, filter() with lambda functions, and for loops. Using List Comprehension List comprehension provides a concise and elegant way to filter out strings from tuples. This approach creates a new list by iterating through the original list and applying a condition to each element ? def remove_strings_list_comprehension(tuples_list): return [tuple(element for element in t if not isinstance(element, ...
Read MoreHow to check for spaces in Python string?
Checking for spaces (whitespaces) in Python strings is a common task when processing text data. Python provides several built-in methods to detect spaces in strings, each suitable for different scenarios. Using isspace() Method The isspace() method checks if a character at a specific index is a whitespace character. It returns True if the character is a space, tab, or newline. Example # Check for space at a specific index text = "Welcome to Tutorialspoint" # Check if character at index 7 is a space if text[7].isspace(): print("Space found at index ...
Read MorePython – Alternate front – rear sum
One of the most important data types is List in Python. Python provides various built-in methods to manipulate list items like append(), insert(), extend() and so on. Multiple approaches help in finding the alternate front-rear sum of an array. The process involves adding the first element to the last element, the second element to the second-last element, and so on. For example, with the array [40, 50, 20, 30], the alternate front-rear sum is calculated as (40+30) + (50+20) = 140. Using Conditional Statement This approach uses a while loop with a conditional statement to pair elements ...
Read MorePython – Check for float String
Python strings consist of characters, and we often need to verify if a string represents a valid float value. This is useful for input validation, data parsing, and type checking. Python provides several approaches to check if a string contains a float value. Method 1: Using try-except Block This approach attempts to convert the string to a float and catches any ValueError exceptions ? def is_float_string(text): try: # First check if it's an integer int(text) ...
Read MoreBlackman in Python Numpy
The Blackman window is a widely used window function in signal processing that helps reduce spectral leakage effects. NumPy provides efficient array operations to implement this window function using its mathematical formula and vectorized operations. In this article, we'll explore three different methods to implement the Blackman window in Python using NumPy. Each approach demonstrates different programming techniques while achieving the same result. Blackman Window Formula The Blackman window is defined by the formula: w(n) = 0.42 - 0.5 * cos(2πn/(N-1)) + 0.08 * cos(4πn/(N-1)) Where n is the sample index (0 to N-1) and ...
Read MoreHow to Append suffix/prefix to strings in a Python list?
In Python, you often need to add prefixes or suffixes to all strings in a list. This is common when formatting data, adding file extensions, or modifying text elements. Python provides several built-in methods like map(), list comprehensions, and reduce() to accomplish this efficiently. Using map() Function The map() function applies a given function to each item in a list. Combined with lambda expressions, it's perfect for adding prefixes and suffixes ? # Original list of strings items = ['note', 'book', 'pen'] suffix = 's' prefix = 'my_' # Adding suffix using map() and lambda ...
Read MorePython – Aggregate values by tuple keys
When working with data in Python, you often need to aggregate values by tuple keys — combining values that share the same tuple identifier. This is useful for grouping data by multiple attributes and performing calculations like sums, averages, or counts. Using defaultdict() Method The defaultdict class from the collections module provides an efficient way to aggregate values by automatically handling missing keys ? from collections import defaultdict # Sample data: (product, cost) tuples item_data = [ ('Milk', 30), ('Tomato', 100), ('Lentils', 345), ...
Read More