
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

151 Views
When it is required to sort a matrix by ‘None’ frequency, a method is defined that takes a parameter and uses list comprehension, ‘not’ operator and ‘len’ method to determine the result.ExampleBelow is a demonstration of the same −def get_None_freq(row): return len([element for element in row if not element]) my_list = [[None, 24], [None, 33, 3, None], [42, 24, 55], [13, None, 24]] print("The list is : ") print(my_list) my_list.sort(key = get_None_freq) print("The result is : ") print(my_list)OutputThe list is : [[None, 24], [None, 33, 3, None], [42, 24, 55], [13, None, 24]] The result ... Read More

279 Views
When it is required to extract range of consecutive similar elements ranges from list, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = [12, 23, 23, 23, 48, 48, 36, 17, 17] print("The list is : ") print(my_list) my_result = [] index = 0 while index < (len(my_list)): start_position = index val = my_list[index] while (index < len(my_list) and my_list[index] == val): index += 1 end_position = index - 1 my_result.append((val, start_position, end_position)) print("The my_result is :") print(my_result)OutputThe ... Read More

254 Views
When it is required to filter tuples with strings that have specific characters, a list comprehension and the ‘all’ operator is used.ExampleBelow is a demonstration of the same −my_list = [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] print("The list is :") print(my_list) char_string = 'pyestb' my_result = [index for index in my_list if all(all(sub in char_string for sub in element) for element in index)] print("The result is : ") print(my_result)OutputThe list is : [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] The result is : [('pyt', 'best')]ExplanationA list of tuple is defined and displayed on the console.A string is ... Read More

167 Views
When it is required to filter rows with elements which are multiples of K, a list comprehension and modulus operator are used.ExampleBelow is a demonstration of the same −my_list = [[15, 10, 25], [14, 28, 23], [120, 55], [55, 30, 203]] print("The list is :") print(my_list) K = 5 print("The value of K is ") print(K) my_result = [index for index in my_list if all(element % K == 0 for element in index)] print("The result is :") print(my_result)OutputThe list is : [[15, 10, 25], [14, 28, 23], [120, 55], [55, 30, 203]] The value of K ... Read More

579 Views
When it is required to find character indices that map to a string list, a simple iteration, list comprehension and ‘add’ method is used.ExampleBelow is a demonstration of the same −from collections import defaultdict my_list = ['p y t h o n', 'i s', 'f u n', 't o', 'l e a r n'] print("The list is :") print(my_list) my_result = defaultdict(set) for index, element in enumerate(my_list): for sub in element.split(): my_result[sub].add(index + 1) my_result = {key: list(val) for key, val in my_result.items()} print("The result is :") print(my_result)OutputThe list is ... Read More

496 Views
When it is required to extract dictionaries with values sum greater than K, a simple iteration and ‘if’ condition is used.ExampleBelow is a demonstration of the same −my_list = [{"Python" : 14, "is" : 18, "fun" : 19}, {"Python" : 12, "is": 4, "fun" : 16}, {"Python" : 13, "is": 17, "fun" : 11}, {"Python" : 13, "is": 16, "fun" : 13}] print("The list :") print(my_list) K =35 print("The value for K :") print(K) my_result = [] for index in my_list: sum = 0 for key in index: sum += index[key] ... Read More

986 Views
When it is required to map the matrix to a dictionary, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("The list :") print(my_list) map_dict = {2 : "Python", 1: "fun", 3 : "to", 4 : "learn"} my_result = [] for index in my_list: temp = [] for element in index: temp.append(map_dict[element]) my_result.append(temp) print("The result is :") print(my_result)OutputThe list : [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] The result is : [['Python', 'learn', ... Read More

345 Views
When it is required to convert an integer matrix to a string matrix, a list comprehension is used.ExampleBelow is a demonstration of the same −my_list = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] print("The list is :") print(my_list) my_result = [[str(element) for element in index]for index in my_list] print("The reuslt is :") print(my_result)OutputThe list is : [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] The reuslt is : [['14', '25', '17'], ['40', '28', '13'], ['59', '44', '66'], ['29', '33', '16']]ExplanationA list is defined and displayed on the console.A ... Read More

481 Views
When it is required to extract characters in given range from a string list, a list comprehension and list slicing is used.ExampleBelow is a demonstration of the same −my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ''.join([element for element in my_list])[start : end] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : tolearnExplanationA list is defined and displayed on the console.The value for ‘start’ and ‘end’ are defined.A list comprehension is used to iterate over the list, ... Read More

416 Views
When it is required to sort a list by factor count, a method is defined that uses list comprehension and modulus operator along with ‘len’ method to determine the output.ExampleBelow is a demonstration of the same −def factor_count(element): return len([element for index in range(1, element) if element % index == 0]) my_list = [121, 1120, 13540, 221, 1400] print("The list is :") print(my_list) my_list.sort(key=factor_count) print("The result is :") print(my_list)OutputThe list is : [121, 1120, 13540, 221, 1400] The result is : [121, 221, 13540, 1120, 1400]ExplanationA method named ‘factor_count’ is defined that takes element of ... Read More