Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Test if all rows contain any common element with other Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 276 Views

When working with matrices (lists of lists), you might need to check if all corresponding rows share at least one common element. This can be achieved using iteration with a flag variable to track the result. Example Below is a demonstration of testing row-wise common elements between two matrices − matrix_1 = [[3, 16, 1], [2, 4], [4, 31, 31]] matrix_2 = [[42, 16, 12], [42, 8, 12], [31, 7, 10]] print("The first matrix is :") print(matrix_1) print("The second matrix is :") print(matrix_2) result = True for idx in range(len(matrix_1)): ...

Read More

Python – Reorder for consecutive elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 212 Views

When working with lists in Python, you may need to reorder consecutive elements by grouping identical values together. This can be achieved using the Counter class from the collections module along with list iteration. What is Reordering for Consecutive Elements? Reordering for consecutive elements means rearranging a list so that all identical values appear together consecutively, while maintaining their original frequency count. Using Counter for Reordering The Counter class counts the frequency of each element, then we can reconstruct the list with grouped elements ? from collections import Counter my_list = [21, 83, ...

Read More

Python program to remove each y occurrence before x in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 161 Views

When it is required to remove every 'y' occurrence before 'x' in a list, a list comprehension along with the 'index' method are used. This technique helps filter out specific values that appear before a target element. Problem Statement Given a list of elements, we need to remove all occurrences of value 'y' that appear before the first occurrence of value 'x'. Elements after 'x' remain unchanged. Example Below is a demonstration of removing all occurrences of 4 before the first occurrence of 8 − my_list = [4, 45, 75, 46, 66, 77, 48, ...

Read More

Python Program that prints the rows of a given length from a matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 201 Views

When working with matrices (lists of lists), you often need to filter rows based on their length. Python provides several approaches to extract rows of a specific length from a matrix. Using List Comprehension List comprehension offers a concise way to filter rows by length − matrix = [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] print("The matrix is:") print(matrix) target_length = 4 result = [row for row in matrix if len(row) == target_length] print("Rows with length", target_length, ":") print(result) The ...

Read More

Python Program to print a specific number of rows with Maximum Sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 259 Views

When working with a list of lists, you often need to find the rows with the highest sum values. Python provides an elegant solution using the sorted() method combined with a lambda function to sort rows by their sum in descending order. Example Below is a demonstration of finding the top 3 rows with maximum sum ? my_list = [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] print("The list is:") print(my_list) my_key = 3 print("The number of top rows to select:") print(my_key) my_result = sorted(my_list, key=lambda ...

Read More

Python – Sort Matrix by Palindrome count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 287 Views

When it is required to sort a matrix based on palindrome count, a method is defined that takes a list as parameter. It uses list comprehension and the join() method to iterate and check if an element is a palindrome or not. Based on this count, the matrix rows are sorted and displayed. What is a Palindrome? A palindrome is a word that reads the same forwards and backwards, like "level", "noon", or "abcba". Example Below is a demonstration of sorting a matrix by palindrome count − def get_palindrome_count(row): return ...

Read More

Python Program to return the Length of the Longest Word from the List of Words

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

When it is required to return the length of the longest word from a list of words, a method is defined that takes a list as parameter. It iterates through each word to find the maximum length and returns it. Example Below is a demonstration of the same ? def find_longest_length(word_list): max_length = len(word_list[0]) temp = word_list[0] for element in word_list: if len(element) > max_length: ...

Read More

Python Program to Sort Matrix Rows by summation of consecutive difference of elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 241 Views

This article demonstrates how to sort matrix rows by the summation of consecutive differences between adjacent elements. We calculate the absolute difference between consecutive elements in each row, sum them up, and use this sum as the sorting key. Understanding Consecutive Differences For a row like [97, 6, 47, 3], the consecutive differences are − |6 - 97| = 91 |47 - 6| = 41 |3 - 47| = 44 Sum = 91 + 41 + 44 = 176 Example Here's a complete program that sorts matrix rows by their consecutive difference summation ...

Read More

Python – Append given number with every element of the list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 392 Views

When you need to add a specific number to every element in a list, Python offers several approaches. The most common method uses list comprehension, which provides a concise and readable solution. Using List Comprehension List comprehension allows you to create a new list by applying an operation to each element ? numbers = [25, 36, 75, 36, 17, 7, 8, 0] print("Original list:") print(numbers) append_value = 6 result = [x + append_value for x in numbers] print("List after appending", append_value, "to each element:") print(result) Original list: [25, 36, 75, ...

Read More

Python – Cumulative Row Frequencies in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 367 Views

When you need to count how many times specific elements appear in each row of a nested list, you can use Python's Counter class from the collections module combined with list comprehension to calculate cumulative row frequencies. What are Cumulative Row Frequencies? Cumulative row frequencies refer to counting the total occurrences of specified elements within each row of a nested list. For example, if you want to find how many times elements [19, 2, 71] appear in each row, you sum their individual frequencies per row. Example Here's how to calculate cumulative row frequencies using Counter ...

Read More
Showing 3781–3790 of 61,297 articles
« Prev 1 377 378 379 380 381 6130 Next »
Advertisements