Python Articles

Page 8 of 854

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 280 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 216 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 205 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 458 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 232 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 267 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 371 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 210 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

Python – Sort Tuples by Total digits

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 324 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 −Exampledef 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), (12345, ...

Read More
Showing 71–80 of 8,532 articles
« Prev 1 6 7 8 9 10 854 Next »
Advertisements