Server Side Programming Articles

Page 334 of 2109

Python program to construct Equidigit tuples

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 157 Views

Equi-digit tuples are tuples where a number is split into two halves at the middle position. Python provides an efficient way to construct these using the floor division operator // and string slicing. Understanding Equi-digit Tuples An equi-digit tuple divides a number into two parts of equal or nearly equal length. For even-digit numbers, both parts have equal digits. For odd-digit numbers, the first part has one fewer digit than the second part. Example Below is a demonstration of constructing equi-digit tuples ? my_list = [5613, 1223, 966143, 890, 65, 10221] print("The list ...

Read More

Python program to omit K length Rows

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 249 Views

When working with lists of lists, you may need to filter out rows based on their length. This Python program demonstrates how to omit rows that have exactly K elements using iteration and the len() method. Example Below is a demonstration of the same − my_list = [[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]] print("The list is :") print(my_list) my_k = 2 print("The value of K is") print(my_k) my_result = [] for row in my_list: if len(row) != my_k : ...

Read More

Python program to extract rows with common difference elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 167 Views

When it is required to extract rows with common difference elements (arithmetic progression), we can iterate through each row and check if consecutive elements have the same difference. A flag value is used to track whether a row maintains constant difference throughout. Understanding Common Difference A sequence has common difference when the difference between consecutive elements remains constant. For example, in [11, 12, 13], the difference is always 1. Example Below is a demonstration of extracting rows with common difference − data_list = [[31, 27, 10], [8, 11, 12], [11, 12, 13], [6, 9, ...

Read More

Python – Test if tuple list has a single element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 374 Views

When it is required to test if a tuple list contains a single unique element across all tuples, we can use different approaches. This means checking if all elements in all tuples are the same value. Using Nested Loop with Flag The traditional approach uses nested loops with a flag variable to track whether all elements are identical ? my_list = [(72, 72, 72), (72, 72), (72, 72)] print("The list is :") print(my_list) my_result = True for sub in my_list: flag = True for element in ...

Read More

Python – Redistribute Trimmed Values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 107 Views

When you need to redistribute trimmed values, you remove elements from both ends of a list and distribute their sum evenly across the remaining elements. This technique uses list slicing and the division operator. What is Redistributing Trimmed Values? Redistributing trimmed values means: Remove a specified number of elements from both ends of a list Calculate the sum of the removed (trimmed) elements Distribute this sum evenly among the remaining elements Example Below is a demonstration of redistributing trimmed values ? my_list = [11, 26, 24, 75, 96, 37, 48, 29, 93] ...

Read More

Python – Consecutive identical elements count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

When working with lists in Python, you might need to count how many distinct values appear consecutively (repeated adjacent elements). This can be achieved using iteration, the append() method, and set() to find unique consecutive elements. Example Here's how to count distinct consecutive identical elements in a list − my_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] == my_list[index + 1]: ...

Read More

Python – Rows with all List elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 812 Views

When it is required to find rows that contain all elements from a given list, a flag value, simple iteration and the 'append' method can be used. This is useful for filtering data based on multiple criteria. Example Below is a demonstration of finding rows containing all specified elements ? my_list = [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True ...

Read More

Python – Filter rows without Space Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 265 Views

When working with nested lists containing strings, you might need to filter out rows that contain any strings with spaces. Python provides several approaches to accomplish this task using list comprehensions and string checking methods. Using Regular Expressions The most robust approach uses the re module to search for whitespace characters ? import re data = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The original list is:") print(data) result = [row for row in data if not any(bool(re.search(r"\s", element)) for element in row)] print("Rows without space strings:") print(result) ...

Read More

Python – Equidistant consecutive characters Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 288 Views

Equidistant consecutive character strings are strings where the ASCII difference between consecutive characters remains constant throughout the string. For example, in "abc", each character is 1 ASCII value apart, while in "agms", each character is 6 ASCII values apart. Understanding the Concept To check if a string has equidistant consecutive characters, we need to ? Calculate the ASCII difference between the first two characters Verify that all other consecutive pairs have the same difference Use ord() to get ASCII values and all() to check the condition Example Here's how to find all equidistant ...

Read More

Python – Reform K digit elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 210 Views

When working with lists of numbers, you may need to reform K digit elements by concatenating all numbers into a string and then splitting them into chunks of K digits. This technique uses string manipulation and list comprehension. Example Below is a demonstration of reforming elements into 3-digit chunks − my_list = [231, 67, 232, 1, 238, 31, 793] print("The list is :") print(my_list) K = 3 print("The value of K is") print(K) # Join all numbers as a single string temp = ''.join([str(ele) for ele in my_list]) print("Combined string:", temp) my_result ...

Read More
Showing 3331–3340 of 21,090 articles
« Prev 1 332 333 334 335 336 2109 Next »
Advertisements