
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 10476 Articles for Python

208 Views
When it is required to filter rows without the soace strings, a list comprehension, regular expression, the ‘not’ operator and the ‘any’ method are used.ExampleBelow is a demonstration of the sameimport re my_list = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The list is :") print(my_list) my_result = [row for row in my_list if not any(bool(re.search(r"\s", element)) for element in row)] print("The resultant list is :") print(my_result)OutputThe list is : [[‘python is’, ‘fun’], [‘python’, ‘good’], [‘python is cool’], [‘love’, ‘python’]] The resultant list is : [[‘python’, ‘good’], [‘love’, ‘python’]]ExplanationThe required packages are imported ... Read More

235 Views
When it is required to find equidistant consecutive character strings, a list comprehension, the ‘all’ operator and the ‘ord’ method are used.ExampleBelow is a demonstration of the samemy_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms'] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(ord(sub[index + 1]) - ord(sub[index]) == ord(sub[1]) - ord(sub[0]) for index in range(0, len(sub) - 1))] print("The resultant list is :") print(my_result)OutputThe list is : ['abc', 'egfg', 'mpsv', 'abed', 'xzbd', 'agms'] The resultant list is : ['abc', 'mpsv', 'agms']ExplanationA list of string values is defined and is displayed on ... Read More

163 Views
When it is required to reform K digit elements, a list comprehension and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [231, 67, 232, 1, 238, 31, 793] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) temp = ''.join([str(ele) for ele in my_list]) my_result = [] for index in range(0, len(temp), K): my_result.append(int(temp[index: index + K])) print("The resultant list is :") print(my_result)OutputThe list is : [231, 67, 232, 1, 238, 31, 793] The value of K is 3 The resultant ... Read More

266 Views
When it is required to check if all ‘y’ occurs after ‘x’ in a list, the enumerate attribute along with a specific condition is used.ExampleBelow is a demonstration of the samemy_list = [11, 25, 13, 11, 64, 25, 8, 9] print("The list is :") print(my_list) x, y = 13, 8 x_index = my_list.index(x) my_result = True for index, element in enumerate(my_list): if element == y and index < x_index: my_result = False break if(my_result == True): ... Read More

225 Views
When it is required to test if all rows contain any common element with other matrix, a simple iteration and a flag value are used.ExampleBelow is a demonstration of the samemy_list_1 = [[3, 16, 1], [2, 4], [4, 31, 31]] my_list_2 = [[42, 16, 12], [42, 8, 12], [31, 7, 10]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = True for idx in range(0, len(my_list_1)): temp = False for element in my_list_1[idx]: if element in my_list_2[idx]: temp = True ... Read More

150 Views
When it is required to reorder consecutive elements, the ‘Counter’ method, an empty list and a simple iteration are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [21, 83, 44, 52, 61, 72, 81, 96, 18, 44] print("The list is :") print(my_list) my_frequencys = Counter(my_list) my_result = [] for value, count in my_frequencys.items(): my_result.extend([value]*count) print("The resultant list is :") print(my_result)OutputThe list is : [21, 83, 44, 52, 61, 72, 81, 96, 18, 44] The resultant list is : [21, 83, 44, 44, 52, 61, 72, 81, 96, 18]ExplanationThe ... Read More

114 Views
When it is required to remove every ‘y’ occurrence before ‘x’ in a list, a list comprehension along with the ‘index’ method are used.ExampleBelow is a demonstration of the samemy_list = [4, 45, 75, 46, 66, 77, 48, 99, 10, 40, 5, 8] print("The list is :") print(my_list) a, b = 8, 4 index_a = my_list.index(a) my_result = [ele for index, ele in enumerate(my_list) if ele != b or (ele == b and index > index_a) ] print("The resultant list is ") print(my_result)OutputThe list is : [4, 45, 75, 46, 66, 77, 48, 99, 10, ... Read More

148 Views
When it is required to print the row of a specific length from a matrix, list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] print("The list is :") print(my_list) my_key = 4 my_result = [sub for sub in my_list if len(sub) == my_key] print("The resultant list is :") print(my_result)OutputThe list is : [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] The resultant list is : [[22, 4, 63, 7]]ExplanationA list ... Read More

202 Views
When it is required to print a specific number of rows with the maximum sum, the ‘sorted’ method, and the ‘lambda’ method are used.ExampleBelow is a demonstration of the samemy_list = [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] print("The list is :") print(my_list) my_key = 3 print("The key is") print(my_key) my_result = sorted(my_list, key=lambda row: sum(row), reverse=True)[:my_key] print("The resultant list is :") print(my_result)OutputThe list is : [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] The key is 3 The resultant list is ... Read More

219 Views
When it is required to sort matrix based on palindrome count, a method is defined that takes a list as parameter. It uses the list comprehension and ‘join’ method to iterate and see if an element is a palindrome or not. Based on this, results are determined and displayed.ExampleBelow is a demonstration of the samedef get_palindrome_count(row): return len([element for element in row if''.join(list(reversed(element))) == element]) my_list = [["abcba", "hdgfue", "abc"], ["peep"], ["py", "is", "best"], ["sees", "level", "non", "noon"]] print("The list is :") print(my_list) my_list.sort(key=get_palindrome_count) print("The resultant list is :") print(my_list)OutputThe list is : [['abcba', 'hdgfue', 'abc'], ... Read More