Articles on Trending Technologies

Technical articles with clear explanations and examples

Python - Merge a Matrix by the Elements of First Column

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 394 Views

When it is required to merge a matrix by the elements of first column, a simple iteration and list comprehension with setdefault method is used. This technique groups rows that have the same value in their first column. Example Below is a demonstration of the same — my_list = [[41, "python"], [13, "pyt"], [41, "is"], [4, "always"], [3, "fun"]] print("The list is :") print(my_list) my_result = {} for key, value in my_list: my_result.setdefault(key, []).append(value) my_result = [[key] + value for key, value in my_result.items()] print("The result is :") ...

Read More

Python – Concatenate Strings in the Given Order

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 434 Views

When you need to concatenate strings in a specific custom order, you can use simple iteration with index-based access. This technique allows you to rearrange and join string elements according to any desired sequence. Basic Example Below is a demonstration of concatenating strings in a custom order − my_list = ["pyt", "fun", "for", "learning"] print("The list is:") print(my_list) sort_order = [1, 0, 3, 2] my_result = '' for element in sort_order: my_result += my_list[element] print("The result is:") print(my_result) The list is: ['pyt', 'fun', 'for', 'learning'] ...

Read More

Python Program to remove elements that are less than K difference away in a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 280 Views

When it is required to remove elements that are less than K difference away in a list, a simple iteration and 'if' condition is used. This technique ensures that any two consecutive elements in the final list have a difference of at least K. Example Below is a demonstration of the same − my_list = [13, 29, 24, 18, 40, 15] print("The list is :") print(my_list) K = 3 my_list = sorted(my_list) index = 0 while index < len(my_list) - 1: if my_list[index] + K > my_list[index ...

Read More

Python – Append List every Nth index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 721 Views

When you need to insert elements from one list at every Nth index position of another list, you can use enumerate() to track indices and conditionally insert elements. Basic Example Here's how to append a list every Nth index ? numbers = [13, 27, 48, 12, 21, 45, 28, 19, 63] append_list = ['P', 'Y', 'T'] N = 3 print("Original list:") print(numbers) print(f"Append list: {append_list}") print(f"N = {N}") result = [] for index, element in enumerate(numbers): if index % N == 0: ...

Read More

Python – Filter Tuples Product greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 277 Views

When working with tuples in Python, you may need to filter tuples based on the product of their elements. This involves calculating the product of all elements in each tuple and keeping only those tuples where the product exceeds a threshold value K. Using List Comprehension with Helper Function The most readable approach is to create a helper function to calculate the product, then use list comprehension for filtering ? def tuples_product(tuple_data): result = 1 for element in tuple_data: result ...

Read More

Python – Adjacent elements in List

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

When working with lists in Python, you often need to find adjacent elements for each item. This involves getting the previous and next elements relative to each position in the list. Understanding Adjacent Elements Adjacent elements are the neighboring items in a list: For the first element, only the next element exists For the last element, only the previous element exists For middle elements, both previous and next elements exist Using enumerate() to Find Adjacent Elements The enumerate() function provides both index and value, making it easy to access neighboring elements ? ...

Read More

Python – Sort by Units Digit in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 452 Views

When sorting a list by its units digit (last digit), we can use a custom key function with the sort() method. This function converts each number to a string and extracts the last character using negative indexing. Using a Custom Key Function The most straightforward approach is to define a helper function that extracts the units digit ? def unit_sort(element): return str(element)[-1] numbers = [716, 134, 343, 24742] print("The list is:") print(numbers) numbers.sort(key=unit_sort) print("The result is:") print(numbers) The list is: [716, 134, 343, 24742] The ...

Read More

Python – Remove rows with Numbers

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 386 Views

When working with nested lists in Python, you may need to remove rows that contain numeric values. This can be achieved using list comprehension combined with the not, any, and isinstance functions. Example Below is a demonstration of removing rows containing numbers ? data_rows = [[14, 'Pyt', 'fun'], ['Pyt', 'is', 'best'], [23, 51], ['Pyt', 'fun']] print("The list is :") print(data_rows) result = [row for row in data_rows if not any(isinstance(element, int) for element in row)] print("The result is :") print(result) Output The list is : [[14, 'Pyt', 'fun'], ['Pyt', ...

Read More

Python - Sort Matrix by Maximum Row element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 286 Views

When you need to sort a matrix by the maximum element in each row, you can use Python's sort() method with a custom key function. This technique is useful for organizing data based on the highest value in each row. Using a Custom Key Function Define a function that returns the maximum element of each row, then use it as the sorting key ? def sort_max(row): return max(row) my_list = [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] print("The original matrix is:") print(my_list) my_list.sort(key=sort_max, reverse=True) ...

Read More

Python – Display the key of list value with maximum range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 303 Views

When it is required to display the key of list value with maximum range, a simple iteration is used. The range of a list is the difference between its maximum and minimum values. Example Below is a demonstration of the same − my_dict = {"pyt" : [26, 12, 34, 21], "fun" : [41, 27, 43, 53, 18], "learning" : [21, 30, 29, 13]} print("The dictionary is :") print(my_dict) max_range = 0 result_key = "" for key, values in my_dict.items(): current_range = max(values) - min(values) if ...

Read More
Showing 3871–3880 of 61,299 articles
« Prev 1 386 387 388 389 390 6130 Next »
Advertisements