Python Articles

Page 39 of 855

Python – Custom Lower bound a List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 527 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 −Examplemy_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 : [51, ...

Read More

Python – Negative index of Element in List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 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 −Examplemy_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 console.A ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 289 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 −Examplemy_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, 12, ...

Read More

Python – Sort Matrix by total characters

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 220 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 −Exampledef 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 that ...

Read More

Python – Elements with factors count less than K

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 212 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 −Exampledef factors(element, K):    return len([index for index in range(1, element + 1) if element % index == 0])

Read More

Python – Elements with same index

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 464 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 −Examplemy_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, and ...

Read More

Python – Extract elements with equal frequency as value

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 241 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 −Examplemy_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 to ...

Read More

Python – Sort by a particular digit count in elements

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 273 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 −Exampledef 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, 4522, ...

Read More

Python – Filter rows with required elements

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 380 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 −Examplemy_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, 49, ...

Read More

Python – Incremental Slice concatenation in String list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 214 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 −Examplemy_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 to ...

Read More
Showing 381–390 of 8,547 articles
« Prev 1 37 38 39 40 41 855 Next »
Advertisements