AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 99 of 840

Python - Find starting index of all Nested Lists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 468 Views

When working with nested lists, you might need to find the starting index of each sublist if all elements were flattened into a single list. This is useful for indexing and data processing tasks. Understanding the Problem Given a nested list like [[51], [91, 22, 36, 44], [25, 25]], if we flatten it to [51, 91, 22, 36, 44, 25, 25], we want to know where each original sublist starts: [0, 1, 5]. Method 1: Using Simple Iteration Track the cumulative length as we iterate through each sublist ? my_list = [[51], [91, 22, ...

Read More

Python - Check if list contain particular digits

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 451 Views

When you need to check if all numbers in a list are composed only of specific digits, you can use string operations and set comparison. This is useful for validating numbers that should only contain certain digits. Example Below is a demonstration of checking if list elements contain only particular digits − numbers = [415, 133, 145, 451, 154] print("The list is:") print(numbers) allowed_digits = [1, 4, 5, 3] # Convert allowed digits to string set for faster lookup digit_string = ''.join([str(digit) for digit in allowed_digits]) all_numbers = ''.join([str(num) for num in numbers]) ...

Read More

Python - Total equal pairs in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 705 Views

When it is required to find the total equal pairs in a list, we can use the set() function along with the floor division operator // and iteration. This approach counts how many pairs can be formed from duplicate elements in the list. Example Below is a demonstration of finding equal pairs in a list − numbers = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print("The list is :") print(numbers) unique_elements = set(numbers) total_pairs = 0 for element in unique_elements: total_pairs ...

Read More

Python program to return rows that have element at a specified index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 259 Views

When it is required to return rows that have an element at a specified index, a simple iteration and the append() function can be used. This technique is useful for filtering rows based on matching elements at specific positions. Basic Example Below is a demonstration of comparing elements at a specific index ? my_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_key = 0 my_result = [] ...

Read More

Python Program to find the Next Nearest element in a Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 231 Views

Finding the next nearest element in a matrix involves searching for a specific value that appears after a given position. This is useful in applications like pathfinding, data analysis, and grid-based algorithms where you need to locate the next occurrence of a value. Algorithm The approach starts from a given position (x, y) and searches row by row for the target element. It first checks the remaining elements in the current row, then moves to subsequent rows. Example Below is a demonstration of finding the next nearest element ? def get_nearest_elem(matrix, x, y, target): ...

Read More

Python program to find the redundancy rates for each row of a matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 380 Views

When it is required to find the redundancy rates for every row of a matrix, a simple iteration and the append method can be used. The redundancy rate represents how many duplicate values exist in each row, calculated as 1 - (unique_elements / total_elements). Understanding Redundancy Rate The redundancy rate formula is ? redundancy_rate = 1 - (number_of_unique_elements / total_elements) A redundancy rate of 0 means all elements are unique, while 1 means all elements are identical. Example Below is a demonstration of finding redundancy rates for each row ? ...

Read More

Python - Change the signs of elements of tuples in a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 428 Views

When it is required to change the signs of elements in a list of tuples, you can use iteration along with the abs() method to manipulate positive and negative values. This technique is useful for standardizing tuple formats or data preprocessing. Example Below is a demonstration of changing tuple elements to have positive first elements and negative second elements − my_list = [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] print("The list is :") print(my_list) my_result = [] for sub in my_list: my_result.append((abs(sub[0]), -abs(sub[1]))) print("The ...

Read More

Python program to convert a list to a set based on a common element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 201 Views

When you need to group sublists that share common elements, you can convert them to sets and merge those with overlapping elements. This technique uses the union method to combine sets and recursion to handle multiple merges. Example Below is a demonstration of merging sublists based on common elements − def common_elem_set(my_set): for index, val in enumerate(my_set): for j, k in enumerate(my_set[index + 1:], index + 1): if val & k: ...

Read More

Python program to convert a list into a list of lists using a step value

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 268 Views

When working with lists in Python, you may need to convert a flat list into a list of lists using a specific step value. This allows you to group elements and create nested structures from your data. Method 1: Using List Slicing with Step Value The most efficient approach uses list slicing to chunk the original list into smaller sublists ? def convert_list_with_step(data, step): result = [] for i in range(0, len(data), step): result.append(data[i:i+step]) return result ...

Read More

Python Program to find the cube of each list element

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

Finding the cube of each list element is a common operation in Python. We can achieve this using several approaches: simple iteration with append(), list comprehension, or the map() function. Method 1: Using Simple Iteration This approach uses a for loop to iterate through each element and append the cubed value to a new list − numbers = [45, 31, 22, 48, 59, 99, 0] print("The list is:") print(numbers) cubed_numbers = [] for num in numbers: cubed_numbers.append(num * num * num) print("The resultant list is:") print(cubed_numbers) ...

Read More
Showing 981–990 of 8,392 articles
« Prev 1 97 98 99 100 101 840 Next »
Advertisements