Pranavnath

Pranavnath

389 Articles Published

Articles by Pranavnath

Page 5 of 39

Python - Remove all occurrences in nested list

Pranavnath
Pranavnath
Updated on 27-Mar-2026 486 Views

When working with nested lists in Python, there are situations where we need to remove all occurrences of a specific element. Whether it's filtering unwanted data or simplifying complex structures, removing elements from nested lists is a common task. In this article, we'll explore different approaches to achieve this goal using recursion and list manipulation techniques. Advantages of Removing All Occurrences in Nested Lists Simplicity and Readability − Python provides clean and intuitive syntax, making it easier to write and understand code. Removing all occurrences in a nested list using Python is straightforward, thanks to its simplicity and ...

Read More

How to check for whitespaces in a Python Lists?

Pranavnath
Pranavnath
Updated on 27-Mar-2026 1K+ Views

Python lists can contain strings with various types of whitespace characters like spaces, tabs, or newlines. This article explores three different approaches to detect whitespace in list elements: using regular expressions, string manipulation, and the isspace() method. Using Regular Expressions The re module provides pattern matching to detect any whitespace character using \s+ regex pattern ? import re # List with various whitespace types my_list = ['hello', 'world ', ' ', '\t', 'python'] pattern = re.compile(r'\s+') for val in my_list: if pattern.search(val): ...

Read More

Range duplication in Python String

Pranavnath
Pranavnath
Updated on 27-Mar-2026 201 Views

Range duplication in Python strings involves copying a specific portion of characters within a string to create a modified version. This technique is useful for text manipulation, pattern generation, and string formatting tasks. Advantages of Range Duplication in Python Strings Selective Substring Modification − Range duplication allows you to modify specific substrings while keeping the rest of the string intact. This is valuable when you need to alter or repeat a specific portion without affecting other parts. Flexible Text Manipulation − You can easily manipulate text by duplicating specific ranges, enabling you to create string variations based ...

Read More

Python Program to count the number of lists in a list of lists

Pranavnath
Pranavnath
Updated on 27-Mar-2026 481 Views

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 More

Altering duplicate values from a given Python list

Pranavnath
Pranavnath
Updated on 27-Mar-2026 440 Views

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 More

Python – Check Element for Range Occurrences

Pranavnath
Pranavnath
Updated on 27-Mar-2026 193 Views

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 More

Remove all strings from a list of tuples in Python

Pranavnath
Pranavnath
Updated on 27-Mar-2026 329 Views

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 More

How to check for spaces in Python string?

Pranavnath
Pranavnath
Updated on 27-Mar-2026 7K+ Views

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 More

Python – Alternate front – rear sum

Pranavnath
Pranavnath
Updated on 27-Mar-2026 245 Views

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 More

Python – Check for float String

Pranavnath
Pranavnath
Updated on 27-Mar-2026 876 Views

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 More
Showing 41–50 of 389 articles
« Prev 1 3 4 5 6 7 39 Next »
Advertisements