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 – Test for all Even elements in the List for the given Range
When it is required to test all even elements in the list for the given range, a simple iteration and the modulus operator is used. Example Below is a demonstration of the same − my_list = [32, 12, 42, 61, 58, 60, 19, 16] print("The list is :") print(my_list) i, j = 2, 7 my_result = True for index in range(i, j + 1): if my_list[index] % 2: my_result = False break ...
Read MorePython – Sort Matrix by Maximum String Length
When working with matrices (lists of lists) containing strings, you might need to sort rows based on the maximum string length in each row. Python provides an elegant solution using the sort() method with a custom key function. Example Here's how to sort a matrix by the maximum string length in each row ? def max_length(row): return max([len(element) for element in row]) my_matrix = [['pyt', 'fun'], ['python'], ['py', 'cool'], ['py', 'ea']] print("The matrix is:") print(my_matrix) my_matrix.sort(key=max_length) print("The result is:") print(my_matrix) The matrix is: [['pyt', 'fun'], ...
Read MorePython – Extract Row with any Boolean True
When working with nested lists or tuples containing Boolean values, you may need to extract only those rows that contain at least one True value. Python's any() function combined with list comprehension provides an elegant solution for this task. Syntax result = [row for row in data if any(row)] Example Here's how to extract rows containing at least one Boolean True ? my_data = [[False, True], [False, False], [True, False, True], [False]] print("The original data is:") print(my_data) my_result = [row for row in my_data if any(element for element in row)] ...
Read MorePython – Sort Tuples by Total digits
When working with tuples containing numeric data, you might need to sort tuples by the total number of digits across all elements. This involves counting digits in each tuple and using that count as the sorting key. Method: Using a Custom Function We can define a function that converts each element to a string, counts the digits, and returns the total ? def count_tuple_digits(row): return sum([len(str(element)) for element in row]) my_tuple = [(32, 14, 65, 723), (13, 26), (12345, ), (137, 234, 314)] print("The original tuple is:") print(my_tuple) my_tuple.sort(key=count_tuple_digits) ...
Read MorePython – Incremental Slice concatenation in String list
When working with string lists, sometimes we need to perform incremental slice concatenation where each string contributes an increasing number of characters. This technique uses list iteration and string slicing to build a concatenated result. Example Here's how to perform incremental slice concatenation ? my_list = ['pyt', 'is', 'all', 'fun'] print("The list is :") print(my_list) my_result = '' for index in range(len(my_list)): my_result += my_list[index][:index + 1] print("The result is :") print(my_result) The output of the above code is ? The list is : ['pyt', ...
Read MorePython – Filter rows with required elements
When it is required to filter rows with required elements, a list comprehension and the all() operator can be used to check if all elements in each row are present in a reference list. Example The following example demonstrates how to filter rows where all elements are present in a check list ? my_list = [[261, 49, 61], [27, 49, 3, 261], [261, 49, 85], [1, 1, 9]] print("The list is :") print(my_list) check_list = [49, 61, 261, 85] my_result = [row for row in my_list if all(element in check_list for element in ...
Read MorePython – Sort by a particular digit count in elements
When it is required to sort by a particular digit count in elements, a method is defined that takes a list element as parameter and uses the count() and str() methods to determine the results. Below is a demonstration of the same − Example def sort_count_digits(element): return str(element).count(str(my_key)) my_list = [4522, 2452, 1233, 2465] print("The list is :") print(my_list) my_key = 2 print("The value of key is") print(my_key) my_list.sort(key=sort_count_digits) print("The result is :") print(my_list) Output The list is : [4522, 2452, 1233, ...
Read MorePython – Extract elements with equal frequency as value
When it is required to extract elements with equal frequency as value, a list comprehension, the count() method and the set() operator are used. This technique finds elements where the number of times they appear in the list equals their actual value. For example, if the number 4 appears exactly 4 times in the list, or 2 appears exactly 2 times, these elements would be extracted. Example Below is a demonstration of extracting elements with equal frequency as value − my_list = [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] print("The ...
Read MorePython – Elements with same index
When working with lists, sometimes you need to find elements whose values match their index positions. Python provides several approaches to accomplish this task using iteration and the enumerate() function. Using enumerate() with Loop The most straightforward approach uses enumerate() to get both index and element, then compares them ? numbers = [33, 1, 2, 45, 41, 13, 6, 9] print("The list is:") print(numbers) result = [] for index, element in enumerate(numbers): if index == element: result.append(element) print("Elements matching their index:") ...
Read MorePython – Elements with factors count less than K
When working with lists of numbers, you may need to filter elements based on their factor count. This tutorial shows how to find elements that have fewer than K factors using list comprehension and the modulus operator. Understanding Factors A factor of a number is any integer that divides the number evenly (with no remainder). For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12. Method: Using List Comprehension We'll create a function to count factors and filter elements accordingly ? def factors(element, K): return len([index for index in range(1, element + 1) if element % index == 0])
Read More