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 1094 of 3363
223 Views
When it is required to combine list with other list elements, a simple iteration and ‘append’ method is used.Below is a demonstration of the same −Example Live Demomy_list_1 = [12, 14, 25, 36, 15] print("The first list is :") print(my_list_1) my_list_2 = [23, 15, 47, 12, 25] print("The second list is :") print(my_list_2) for element in my_list_2 : my_list_1.append(element) print ("The result is :") print(my_list_1)OutputThe first list is : [12, 14, 25, 36, 15] The second list is : [23, 15, 47, 12, 25] The result is : [12, 14, 25, 36, 15, 23, 15, ... Read More
261 Views
When it is required to sort matrix based upon sum of rows, a method is defined that uses ‘sum’ method to determine the result.Below is a demonstration of the same −Example Live Demodef sort_sum(row): return sum(row) my_list = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print("The list is :") print(my_list) my_list.sort(key = sort_sum) print("The result is :") print(my_list)OutputThe list is : [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] The result is : [[12, 41], [34, 51], [54, 36, 22], [32, 15, 67]]ExplanationA method named ‘sort_sum’ is defined that takes a ... Read More
216 Views
When it is required to sort by maximum digit in element, a method is defined that uses ‘str’ and ‘max’ method to determine the result.Below is a demonstration of the same −Example Live Demodef max_digits(element): return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") print(my_list) my_list.sort(key = max_digits) print("The result is :") print(my_list)OutputThe list is : [224, 192, 145, 18, 3721] The result is : [224, 145, 3721, 18, 192]ExplanationA method named ‘max_digits’ is defined that takes element as a parameter, and converts it into a string, and then gets the maximum ... Read More
387 Views
When it is required to calculate the percentage of positive elements of the list, a list comprehension and the ‘len’ method are used.Below is a demonstration of the same −Example Live Demomy_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result = (len([element for element in my_list if element > 0]) / len(my_list)) * 100 print("The result is :") print(my_result)OutputThe list is : [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] The result is : 0ExplanationA list is defined and displayed on the console.A list comprehension is used ... Read More
279 Views
When it is required to filter rows with range elements, a list comprehension and the ‘all’ operator is used to determine the output.Below is a demonstration of the same −Example Live Demomy_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, 31, 5, 8, 7]] print("The list is :") print(my_list) i, j = 2, 5 my_result = [index for index in my_list if all(element in index for element in range(i, j + 1))] print("The result is :") print(my_result)OutputThe list is : [[3, 2, 4, 5, 10], [32, 12, 4, 51, ... Read More
293 Views
When it is required to sort strings by punctuation count, a method is defined that takes a string as a parameter and uses list comprehension and ‘in’ operator to determine the result.Below is a demonstration of the same −Example Live Demofrom string import punctuation def get_punctuation_count(my_str): return len([element for element in my_str if element in punctuation]) my_list = ["python@%^", "is", "fun!", "to@#r", "@#$learn!"] print("The list is :") print(my_list) my_list.sort(key = get_punctuation_count) print("The result is :") print(my_list)OutputThe list is : ['python@%^', 'is', 'fun!', 'to@#r', '@#$learn!'] The result is : ['is', 'fun!', 'to@#r', 'python@%^', '@#$learn!']ExplanationThe required packages ... Read More
243 Views
When it is required to sort matrix by row median, a method is defined that uses the ‘median’ method to determine the result.Below is a demonstration of the same −Example Live Demofrom statistics import median def median_row(row): return median(row) my_list = [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] print("The list is :") print(my_list) my_list.sort(key = median_row) print("The result is :") print(my_list)OutputThe list is : [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] The result is : [[13, 27, 24], [43, 14, 27], [32, 56, 18], [34, ... Read More
987 Views
When it is required to display all the combinations of a dictionary list, a simple list comprehension and the ‘zip’ method along with the ‘product’ method are used.Below is a demonstration of the same −Example Live Demofrom itertools import product my_list_1 = ["python", "is", "fun"] my_list_2 = [24, 15] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) temp = product(my_list_2, repeat = len(my_list_1)) my_result = [{key : value for (key , value) in zip(my_list_1, element)} for element in temp] print("The result is :") print(my_result)OutputThe first list is : ['python', 'is', 'fun'] The ... Read More
244 Views
When it is required to count average digits in a list, a simple iteration, the ‘str’ method and the ‘/’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = [324, 5345, 243, 746, 432, 463, 946787] print("The list is :") print(my_list) sum_digits = 0 for ele in my_list: sum_digits += len(str(ele)) my_result = sum_digits / len(my_list) print("The result is :") print(my_result)OutputThe list is : [324, 5345, 243, 746, 432, 463, 946787] The result is : 3.5714285714285716ExplanationA list is defined and displayed on the console.A variable is initialized to 0.The list ... Read More
293 Views
When it is required to test string in character list and vice-versa, a simple ‘in’ operator and ‘join’ method are used.Below is a demonstration of the same −Example Live Demomy_string = 'python' print("The string is :") print(my_string) my_key = ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] print("The key is ") print(my_key) joined_list = ''.join(my_key) my_result = my_string in joined_list print("The result is :") if(my_result == True): print("The string is present in the character list") else: print("The string is not present in the character list")OutputThe string is : python The key is ['p', ... Read More