
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
Server Side Programming Articles - Page 953 of 2650

286 Views
When it is required to remove rows with duplicate element in a matrix, a list comprehension and the ‘set’ operator is used.ExampleBelow is a demonstration of the same −my_list = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print("The list is :") print(my_list) my_result = [element for element in my_list if len(set(element)) == len(element)] print("The result is :") print(my_result)OutputThe list is : [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] The result is : [[17, 46, 47], [28, 91, 19]]ExplanationA list of list is defined and is displayed on ... Read More

830 Views
When it is required to extract keywords from a list, a simple iteration and the ‘iskeyword’ method is used.ExampleBelow is a demonstration of the same −import keyword my_list = ["python", 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) my_result = [] for element in my_list: for word in element.split(): if keyword.iskeyword(word): my_result.append(word) print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ['is']ExplanationA list of strings is defined and ... Read More

483 Views
When it is required to sort the strings based on a substring range, a method us defined that uses list slicing to determine the result.ExampleBelow is a demonstration of the same −def get_substring(my_string): return my_string[i : j] my_list = ["python", 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) i, j = 1, 3 print("The value of i and j are :") print(str(i)+ ', ' +str(j)) my_list.sort(key=get_substring) print("The result is :") print(my_list)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The value of i and j are : 1, 3 The result ... Read More

396 Views
When it is required to extract string elements from mixed matrix, a list comprehension and the ‘isinstance’ method is used.ExampleBelow is a demonstration of the same −my_list = [[35, 66, 31], ["python", 13, "is"], [15, "fun", 14]] print("The list is :") print(my_list) my_result = [element for index in my_list for element in index if isinstance(element, str)] print("The result is :") print(my_result)OutputThe list is : [[35, 66, 31], ['python', 13, 'is'], [15, 'fun', 14]] The result is : ['python', 'is', 'fun']ExplanationA list of list is defined and displayed on the console.A list comprehension is used to iterate over ... Read More

231 Views
When it is required to replace first ‘K’ elements by ‘N’, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [13, 34, 26, 58, 14, 32, 16, 89] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = 99 print("The value of N is :") print(N) for index in range(K): my_list[index] = N print("The result is :") print(my_list)OutputThe list is : [13, 34, 26, 58, 14, 32, 16, 89] The value of K is : 2 The value of N ... Read More

259 Views
When it is required to insert character in each duplicate string after every ‘K’ elements, a method is defined that uses ‘append’ method, concatenation operator and list slicing.ExampleBelow is a demonstration of the same −def insert_char_after_key_elem(my_string, my_key, my_char): my_result = [] for index in range(0, len(my_string), my_key): my_result.append(my_string[:index] + my_char + my_string[index:]) return str(my_result) my_string = 'PythonToCode' print("The string is :") print(my_string) K = 2 print("The value of K is ") print(K) add_char = ";" print("The result is ... Read More

157 Views
When it is required to display average of digit greater than K, a simple iteration is used.Below is a demonstration of the same −Examplemy_list = [11, 17, 25, 16, 23, 18] print ("The list is :") print(my_list) K = 15 print("The value of K is ") print(K) my_count = 0 for index in my_list : if index > K : my_count = my_count + 1 print ("The result is :") print(my_count)OutputThe list is : [11, 17, 25, 16, 23, 18] The value of K is 15 The result is : 5ExplanationA list is defined and displayed ... Read More

303 Views
When it is required to get next N elements from K value, a simple iteration is used.Below is a demonstration of the same −Examplemy_list = [31, 24, 46, 18, 34, 52, 26, 29] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = 3 print("The value of N is :") print(N) for index in range(K): my_list[index] = N print("The result is :") print(my_list)OutputThe list is : [31, 24, 46, 18, 34, 52, 26, 29] The value of K is : 2 The value of N is : 3 The result is : ... Read More

2K+ Views
When it is required to create dictionary from a list, the ‘fromkeys’ method in the ‘dict’ method is used.ExampleBelow is a demonstration of the same −my_list = ['Hi', 'Will', 'how', 'Python', 'cool'] print("The list is ") print(my_list) my_dict = dict.fromkeys(my_list, "my_value") print(my_dict)OutputThe list is ['Hi', 'Will', 'how', 'Python', 'cool'] {'Hi': 'my_value', 'Will': 'my_value', 'how': 'my_value', 'Python': 'my_value', 'cool': 'my_value'}ExplanationA list of strings is defined and is displayed on the console.The ‘fromkeys’ method present in ‘dict’ is used to convert the elements of the list to dictionary keys.The value for every key is specified here itself.This is assigned to a ... Read More

159 Views
When it is required to remove tuples with difference greater than K, use the abs() method.Below is a demonstration of the same −Examplemy_tuple = [(41, 18), (21,57), (39, 22), (23, 42), (22, 10)] print("The tuple is :") print(my_tuple) K = 20 my_result = [element for element in my_tuple if abs(element[0] - element[1])