
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
Found 26504 Articles for Server Side Programming

175 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

230 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

187 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

5K+ Views
To find the common elements in a Pandas DataFrame, we can use the merge() method with a list of columnsStepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df1.Print the input DataFrame, df1.Create another two-dimensional tabular data, df2.Print the input DataFrame, df2.Find the common elements using merge() method.Print the common DataFrame.Exampleimport pandas as pd df1 = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) df2 = ... Read More

313 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

692 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

233 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

274 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

233 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

348 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