Programming Articles

Page 1071 of 2544

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

AmitDiwan
AmitDiwan
Updated on 06-Sep-2021 227 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" : "93", "is" : "CS", "amazing" : "16"}] print("The list is :") print(my_list) K = "python" print("The value of K is ") print(K) value = "cool" my_result = value in [index[K] for index in my_list] print("The result is :") if(my_result == True):    print("The value ...

Read More

Python – Sort by Uppercase Frequency

AmitDiwan
AmitDiwan
Updated on 06-Sep-2021 353 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", "is", "FUN", "to", "Learn"] print("The list is:") print(my_list) my_list.sort(key=higher_character_sort) print("The result is:") print(my_list)OutputThe list is: ['pyt', 'is', 'FUN', 'to', 'Learn'] The result is: ['pyt', 'is', 'to', 'Learn', 'FUN']ExplanationA method named ‘higher_character_sort’ is defined that tales an element as parameter.A list comprehension is used to iterate over the ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 06-Sep-2021 770 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, 3, 5, 4] my_result = [index for index in my_list if all(int(element) in digit_list for element in str(index))] print("The result is :") print(my_result)OutputThe list is : [3345, 2345, 1698, 2475, 1932] The result is : [3345, 2345]ExplanationA list is defined and is displayed on the console.Another list of ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 06-Sep-2021 283 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 : 35, 34 : 56}, {11 : 75, 70 : 19, 39 : 70}, {1 : 155}, {48 : 29, 17 : 53}] print("The dictionary is :") print(my_dict) my_dict.sort(key = sum_value) print("The result is :") print(my_dict)OutputThe dictionary is : [{34: 56, 44: 35, 21: 13}, {11: 75, 70: ...

Read More

Python – Remove Dictionaries with Matching Values

AmitDiwan
AmitDiwan
Updated on 06-Sep-2021 297 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) my_dict_2 = [{'Hi': 72, "Will": 19}, {"Will": 13, "Hi": 19}] print("The second dictionary is : ") print(my_dict_2) K = "Hi" print("The value of K is ") print(K) temp = { element[K] for element in my_dict_2} my_result = [element for element in my_dict_1 if element[K] not in temp] ...

Read More

Python – Find the frequency of numbers greater than each element in a list

AmitDiwan
AmitDiwan
Updated on 04-Sep-2021 269 Views

When it is required to find the frequency of numbers greater than each element in a list, a list comprehension and the ‘sum’ method is used.Below is a demonstration of the same −Example Live Demomy_list = [24, 13, 72, 22, 12, 47] print("The list is :") print(my_list) my_result = [sum(1 for element in my_list if index

Read More

Python Program to test whether the length of rows are in increasing order

AmitDiwan
AmitDiwan
Updated on 04-Sep-2021 156 Views

When it is required to test whether the length of rows are in increasing order, a simple iteration and a Boolean value is used.Below is a demonstration of the same −Example Live Demomy_list = [[55], [12, 17], [25, 32, 24], [58, 36, 57, 19, 14]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list) - 1) :    if len(my_list[index + 1])

Read More

Python program to find the sum of all even and odd digits of an integer list

AmitDiwan
AmitDiwan
Updated on 04-Sep-2021 2K+ Views

When it is required to find the sum of all even and odd digits of an integer list, a simple iteration and the ‘modulus’ operator are used.Below is a demonstration of the same −Example Live Demomy_list = [369, 793, 2848, 4314, 57467] print("The list is :") print(my_list) sum_odd = 0 sum_even = 0 for index in my_list:    for element in str(index):       if int(element) % 2 == 0:          sum_even += int(element)       else:          sum_odd += int(element) print("The result is :") print("The sum of ...

Read More

Python – Test for Word construction from character list

AmitDiwan
AmitDiwan
Updated on 04-Sep-2021 233 Views

When it is required to test for word construction from character list, the ‘all’ operator and the ‘count’ method is used.Below is a demonstration of the same −Example Live Demomy_list = ['p', 'p', 'y', 't', 'h', 'p', 'p', 'y', 'n', 'y', 'y', 't'] print("The list is :") print(my_list) key = 'pyt' print("The key is :") print(key) my_result = all(key.count(chr)

Read More

Python – Get Every Element from a String List except for a specified letter

AmitDiwan
AmitDiwan
Updated on 04-Sep-2021 441 Views

When it is required to get every element from a list of strings except a specified letter, a list comprehension and the ‘append’ method is used.Below is a demonstration of the same −Example Live Demomy_list = ["hi", "is", "great", "pyn", "pyt"] print("The list is :") print(my_list) my_key = 'n' print("The value for key is ") print(my_key) my_result = [] for sub in my_list:    my_result.append(''.join([element for element in sub if element == my_key])) print("The result is :") print(my_result)OutputThe list is : ['hi', 'is', 'great', 'pyn', 'pyt'] The value for key is n The result is ...

Read More
Showing 10701–10710 of 25,433 articles
Advertisements