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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python program to omit K length Rows
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 MorePython program to extract rows with common difference elements
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 MorePython – Test if tuple list has a single element
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 MorePython – Redistribute Trimmed Values
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 MorePython – Consecutive identical elements count
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 MorePython – Rows with all List elements
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 MorePython – Filter rows without Space Strings
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 MorePython – Equidistant consecutive characters Strings
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 MorePython – Reform K digit elements
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 MorePython program to test if all y occur after x in List
When it is required to check if all 'y' occurs after 'x' in a list, the enumerate function along with a specific condition is used to compare indices. Example Below is a demonstration of the same − my_list = [11, 25, 13, 11, 64, 25, 8, 9] print("The list is :") print(my_list) x, y = 13, 8 x_index = my_list.index(x) my_result = True for index, element in enumerate(my_list): if element == y and index < x_index: my_result = False ...
Read More