Programming Articles - Page 1095 of 3363

Python – Test for all Even elements in the List for the given Range

AmitDiwan
Updated on 06-Sep-2021 08:17:15

213 Views

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.Below is a demonstration of the same −Example Live Demomy_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 print("The result is :") if(my_result == True):    print("All The elements are in the given range") ... Read More

Python – Sort Matrix by Maximum String Length

AmitDiwan
Updated on 06-Sep-2021 08:14:22

307 Views

When it is required to sort matrix by maximum string length, a method is defined that takes a list as parameter and uses list comprehension and the ‘max’ and ‘len’ methods to determine the result.Below is a demonstration of the same −Example Live Demodef 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 )OutputThe matrix is : [['pyt', 'fun'], ['python'], ['py', 'cool'], ['py', 'ea']] The result is : [['py', 'ea'], ['pyt', 'fun'], ['py', 'cool'], ['python']]ExplanationA method ... Read More

Python – Extract Row with any Boolean True

AmitDiwan
Updated on 06-Sep-2021 08:13:16

235 Views

When it is required to extract row with any Boolean True, a list comprehension is used along with the ‘any’ operator.Below is a demonstration of the same −Example Live Demomy_tuple = [[False, True], [False, False], [True, False, True], [False]] print("The tuple is :") print(my_tuple) my_result = [row for row in my_tuple if any(element for element in row)] print("The result is ") print(my_result)OutputThe tuple is : [[False, True], [False, False], [True, False, True], [False]] The result is [[False, True], [True, False, True]]ExplanationA list of list is defined and displayed on the console.A list comprehension is used to check if ... Read More

Python – Sort Tuples by Total digits

AmitDiwan
Updated on 06-Sep-2021 08:12:13

306 Views

When it is required to sort tuples by total digits, a method is defined that converts every element in the list to a string, and gets the length of each of these strings, and adds them up. This is displayed as result of the method.Below is a demonstration of the same −Example Live Demodef 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 tuple is :") print(my_tuple) my_tuple.sort(key = count_tuple_digits) print("The result is :") print(my_tuple)OutputThe tuple is : [(32, 14, 65, 723), (13, 26), ... Read More

Python – Incremental Slice concatenation in String list

AmitDiwan
Updated on 06-Sep-2021 08:09:57

199 Views

When it is required to display incremental slice concatenation in string list, a simple iteration and list slicing is used.Below is a demonstration of the same −Example Live Demomy_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)OutputThe list is : ['pyt', 'is', 'all', 'fun'] The result is : pisallfunExplanationA list is defined and displayed on the console.An empty string is created.The list is iterated over, and the element is concatenated with the consecutive element.This result is assigned ... Read More

Python – Filter rows with required elements

AmitDiwan
Updated on 06-Sep-2021 08:07:14

350 Views

When it is required to filter rows with required elements, a list comprehension and the ‘all’ operator is used.Below is a demonstration of the same −Example Live Demomy_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 = [index for index in my_list if all(element in check_list for element in index)] print("The result is :") print(my_result)OutputThe list is : [[261, 49, 61], [27, 49, 3, 261], [261, 49, 85], [1, 1, 9]] The result is : [[261, 49, 61], [261, ... Read More

Python – Sort by a particular digit count in elements

AmitDiwan
Updated on 06-Sep-2021 08:06:14

255 Views

When it is required to sort by a particular digit count in elements, a method is defined that takes list element as parameter and uses the ‘count’ and ‘str’ methods to determine the results.Below is a demonstration of the same −Example Live Demodef sort_count_digits(elements):    return str(elements).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)OutputThe list is : [4522, 2452, 1233, 2465] The value of key is 2 The result is : [1233, 2465, ... Read More

Python – Extract elements with equal frequency as value

AmitDiwan
Updated on 06-Sep-2021 08:05:10

209 Views

When it is required to extract elements with equal frequency as value, a list comprehension, the ‘count’ method and the ‘set’ operator are used.Below is a demonstration of the same −Example Live Demomy_list = [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] print("The list is :") print(my_list) my_result = list(set([element for element in my_list if my_list.count(element) == element])) print("The result is :") print(my_result)OutputThe list is : [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] The result is : [2, 4]ExplanationA list is defined and displayed on the console.A list comprehension is used ... Read More

Python – Elements with same index

AmitDiwan
Updated on 06-Sep-2021 08:04:04

443 Views

When it is required to display elements with same index, a simple iteration and the ‘enumerate’ attribute is used.Below is a demonstration of the same −Example Live Demomy_list = [33, 1, 2, 45, 41, 13, 6, 9] print("The list is :") print(my_list) my_result = [] for index, element in enumerate(my_list):    if index == element:       my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [33, 1, 2, 45, 41, 13, 6, 9] The result is : [1, 2, 6]ExplanationA list is defined and displayed on the console.An empty list is defined.The list is iterated over, ... Read More

Python – Elements with factors count less than K

AmitDiwan
Updated on 06-Sep-2021 08:02:27

194 Views

When it is required to display elements with factors count less than K, a method is defined that takes two parameters and uses list comprehension to iterate over the elements and use ‘modulus’ operator to determine the result.Below is a demonstration of the same −Example Live Demodef factors(element, K):    return len([index for index in range(1, element + 1) if element % index == 0])

Advertisements