AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

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

234 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 ... Read More

Python – Negative index of Element in List

AmitDiwan

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 ... Read More

Python – Custom Lower bound a List

AmitDiwan

AmitDiwan

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

481 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) ... Read More

Python – Remove Elements in K distance with N

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:53:32

173 Views

When it is required to remove elements, which are at K distance with N, a list comprehension along with a specific condition is used.Below is a demonstration of the same −Example Live Demomy_list = [13, 52, 5, 45, 65, 61, 18 ] print("The list is :") print(my_list) K = ... Read More

Python – Disjoint Strings across Lists

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:51:57

229 Views

When it is required to find disjoint strings across lists, a method is defined that takes two parameters, and uses the lambda and reduce methods with the ‘if’ condition to determine the result.Below is a demonstration of the same −Example Live Demofrom functools import reduce def determine_disjoint_pairs(disjoint_data, my_result=[]):   ... Read More

Python – Check if particular value is present corresponding to K key

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:50:18

183 Views

When it is required to check if particular value is present corresponding to a key ‘K’, a list comprehension is used.Below is a demonstration of the same −Example Live Demomy_list = [{"python" : "14", "is" : "great", "fun" : "1`"}, {"python" : "cool", "is" : "fun", "best" : "81"}, {"python" : ... Read More

Python – Sort by Uppercase Frequency

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:48:38

312 Views

When it is required to sort the elements of the list by frequency of uppercase elements, a method is defined that uses list comprehension and the ‘isupper’ method.Below is a demonstration of the same −Example Live Demodef higher_character_sort(sub):    return len([ele for ele in sub if ele.isupper()]) my_list = ["pyt", ... Read More

Python program to extract only the numbers from a list which have some specific digits

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:46:11

687 Views

When it is required to extract only the numbers from a list which have some specific digits, a list comprehension and the ‘all’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = [3345, 2345, 1698, 2475, 1932] print("The list is :") print(my_list) digit_list = [2, ... Read More

Python program to Sort a List of Dictionaries by the Sum of their Values

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:17:27

231 Views

When it is required to sort a list of dictionaries based on the sum of their values, a method is defined that uses the ‘sum’ method to determine the result.Below is a demonstration of the same −Example Live Demodef sum_value(row):    return sum(list(row.values())) my_dict = [{21 : 13, 44 : ... Read More

Python – Remove Dictionaries with Matching Values

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 07:12:06

271 Views

When it is required to remove dictionaries with matching values, a dictionary comprehension is used.Below is a demonstration of the same −Example Live Demomy_dict_1 = [{'Hi': 32, "there": 32, "Will":19}, {'Hi': 19, "there": 100, "Will": 13}, {'Hi': 72, "there": 19, "Will": 72}] print("The first dictionary is : ") print(my_dict_1) ... Read More

Advertisements