Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1097 of 3363
267 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
290 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
254 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
375 Views
When it is required to convert elements in a list of tuples to float values, the ‘isalpha’ method, the ‘float’ method, and a simple iteration is used.Below is a demonstration of the same −Example Live Demomy_list = [("31", "py"), ("22", "226.65"), ("18.12", "17"), ("pyt", "12")] print("The list is :") print(my_list) my_result = [] for index in my_list: my_temp = [] for element in index: if element.isalpha(): my_temp.append(element) else: my_temp.append(float(element)) my_result.append((my_temp[0], my_temp[1])) print("The result is :") print(my_result)OutputThe list is : ... Read More
145 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])
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
220 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)
429 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
327 Views
When it is required to print the rows from the matrix that have the same element at the given index, a list comprehension and the ‘all’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]] print("The list is :") print(my_list) my_key = 1 print("The key is ") print(my_key) my_result = [element for element in my_list if all(str(i)[my_key] == str(element[0])[my_key] for i in element)] print("The result is :") print(my_result)OutputThe list is : [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, ... Read More
337 Views
When it is required to print the count of either peaks or valleys from a list, a simple iteration and a specific condition is placed.Below is a demonstration of the same −Example Live Demomy_list = [11, 12, 24, 12, 36, 17, 28, 63] print("The list is :") print(my_list) my_result = 0 for index in range(1, len(my_list) - 1): if my_list[index + 1] > my_list[index] < my_list[index - 1] or my_list[index + 1] < my_list[index] > my_list[index - 1]: my_result += 1 print("The result is :") print(my_result)OutputThe list is : [11, 12, 24, ... Read More