Found 26504 Articles for Server Side Programming

Python – Incremental Slice concatenation in String list

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

181 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

325 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

232 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

184 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

412 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

174 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])

Python – Sort Matrix by total characters

AmitDiwan
Updated on 06-Sep-2021 07:59:10

169 Views

When it is required to sort matrix by total characters, a method is defined that uses list comprehension and the ‘sum’ and ‘len’ methods to determine the result.Below is a demonstration of the same −Example Live Demodef total_characters(row):    return sum([len(element) for element in row]) my_list = [["pyt", "is", "fun"], ["python", "fun"], ["py", "4", "good"], ["python"]] print("The list is :") print(my_list) my_list.sort(key=total_characters) print("The result is :") print(my_list)OutputThe list is : [['pyt', 'is', 'fun'], ['python', 'fun'], ['py', '4', 'good'], ['python']] The result is : [['python'], ['py', '4', 'good'], ['pyt', 'is', 'fun'], ['python', 'fun']]ExplanationA method named ’total_characters’ is defined ... Read More

Python – Extract list with difference in extreme values greater than K

AmitDiwan
Updated on 06-Sep-2021 07:57:35

236 Views

When it is required to extract list with difference in extreme values greater than K, a list comprehension and the ‘min’ and ‘max’ methods are used.Below is a demonstration of the same −Example Live Demomy_list = [[13, 52, 11], [94, 12, 21], [23, 45, 23], [11, 16, 21]] print("The list is :") print(my_list) key = 40 my_result = [element for element in my_list if max(element) - min(element) > key] print("The result is :") print(my_result)OutputThe list is : [[13, 52, 11], [94, 12, 21], [23, 45, 23], [11, 16, 21]] The result is : [[13, 52, 11], [94, ... Read More

Python – Negative index of Element in List

AmitDiwan
Updated on 06-Sep-2021 07:56:42

1K+ Views

When it is required to get the negative index of an element in a list, the ‘len’ method and the ‘index’ method are used.Below is a demonstration of the same −Example Live Demomy_list = [52, 47, 18, 22, 23, 57, 13] print("The list is :") print(my_list) my_key = 22 print("The value of key is ") print(my_key) my_result = len(my_list) - my_list.index(my_key) print("The result is :") print(my_result)OutputThe list is : [52, 47, 18, 22, 23, 57, 13] The value of key is 22 The result is : 4ExplanationA list of integers is defined and is displayed on the ... Read More

Python – Custom Lower bound a List

AmitDiwan
Updated on 06-Sep-2021 07:54:32

484 Views

When it is required to customize the lower bound on a list, a list comprehension can be used and a specific condition can be placed in it.Below is a demonstration of the same −Example Live Demomy_list = [51, 71, 86, 21, 11, 35, 67] print("The list is :") print(my_list) K = 50 print("The value of K is ") print(K) my_result = [element if element >= K else K for element in my_list] print("The result is :") print(my_result)OutputThe list is : [51, 71, 86, 21, 11, 35, 67] The value of K is 50 The result is : ... Read More

Advertisements