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 1096 of 3366
192 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 −Example Live Demomy_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 ... Read More
419 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 −Example Live Demomy_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, ... Read More
184 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 −Example Live Demodef factors(element, K): return len([index for index in range(1, element + 1) if element % index == 0])
181 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 −Example Live Demodef 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 ... Read More
247 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 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, ... Read More
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 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 ... Read More
493 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) 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 : ... Read More
183 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 = 3 print("The value of K is ") print(K) N = 5 print("The value of N is ") print(N) my_result = [element for element in my_list if element < N - K or element > N + K] print("The result is:") print(my_result)OutputThe list is : [13, 52, 5, ... Read More
239 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=[]): if not disjoint_data and not reduce(lambda a, b: set(a) & set(b), my_result): yield tuple(my_result) elif disjoint_data: yield [idx for k in disjoint_data[0] for idx in determine_disjoint_pairs(disjoint_data[1:], my_result + [k])] my_list_1 = ["python", "is", "fun"] my_list_2 = ["its", "awesome", "learning"] ... Read More
198 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