Server Side Programming Articles

Page 343 of 2109

Python – Extract dictionaries with values sum greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 542 Views

Sometimes we need to filter a list of dictionaries based on the sum of their values. This is useful when working with data where you want to find records whose total values exceed a certain threshold. Using Loop Iteration The most straightforward approach is to iterate through each dictionary and calculate the sum of its values ? student_scores = [ {"Math": 14, "Science": 18, "English": 19}, {"Math": 12, "Science": 4, "English": 16}, {"Math": 13, "Science": 17, "English": 11}, {"Math": 13, ...

Read More

Python – Mapping Matrix with Dictionary

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

When working with matrices (lists of lists) in Python, you often need to map numerical values to meaningful strings using a dictionary. This technique is useful for converting coded data into readable format. Basic Matrix Mapping Here's how to map matrix elements using a dictionary with nested loops − my_matrix = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("Original matrix:") print(my_matrix) # Mapping dictionary map_dict = {2: "Python", 1: "fun", 3: "to", 4: "learn"} # Map matrix elements result = [] for row in my_matrix: mapped_row ...

Read More

Python – Convert Integer Matrix to String Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 416 Views

When working with matrices in Python, you may need to convert an integer matrix to a string matrix for display purposes or string operations. Python provides several approaches to accomplish this conversion efficiently. Using List Comprehension The most pythonic way is using nested list comprehension to iterate through the matrix and convert each element ? matrix = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] print("Original integer matrix:") print(matrix) # Convert each integer to string using nested list comprehension string_matrix = [[str(element) for element in row] for row in matrix] ...

Read More

Python program to extract characters in given range from a string list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 570 Views

When it is required to extract characters in a given range from a string list, we can use list comprehension and string slicing. This technique allows us to join all strings and extract a specific portion based on character positions. Example Below is a demonstration of extracting characters from position 11 to 25 ? my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ''.join([element for element in my_list])[start : end] print("The result is :") print(my_result) Output The list ...

Read More

Python – Sort a List by Factor count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 479 Views

When you need to sort a list by the number of factors each element has, you can use a custom function with Python's sort() method. This technique uses list comprehension and the modulus operator to count factors efficiently. Understanding Factor Count A factor of a number is any integer that divides the number evenly (remainder is 0). For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12. Example Here's how to sort a list by factor count ? def factor_count(element): return len([element for index in range(1, ...

Read More

Python – Random range in a List

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

When you need to generate a list of random numbers within a specific range, Python's random.randrange() method combined with list comprehension provides an efficient solution. Syntax The random.randrange() function generates random integers within a specified range ? random.randrange(start, stop, step) start − Starting value (inclusive) stop − Ending value (exclusive) step − Step size (optional, default is 1) Basic Example Generate 10 random numbers between 1 and 100 ? import random my_result = [random.randrange(1, 100, 1) for i in range(10)] print("The result is :") print(my_result) ...

Read More

Python – Extract Sorted Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 262 Views

When you need to filter strings that have their characters in sorted order, you can use list comprehension with the sorted() method. A sorted string is one where the characters appear in alphabetical order. Example Below is a demonstration of extracting sorted strings from a list − my_list = ["pyt", "Fdf", "Fun"] print("The list is :") print(my_list) my_result = [element for element in my_list if ''.join(sorted(element)) == element] print("The result is :") print(my_result) Output The list is : ['pyt', 'Fdf', 'Fun'] The result is : ['Fdf'] How ...

Read More

Python - Merge a Matrix by the Elements of First Column

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 371 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 389 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 203 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
Showing 3421–3430 of 21,090 articles
« Prev 1 341 342 343 344 345 2109 Next »
Advertisements