Found 26504 Articles for Server Side Programming

Python Program to test whether the length of rows are in increasing order

AmitDiwan
Updated on 04-Sep-2021 11:21:21

123 Views

When it is required to test whether the length of rows are in increasing order, a simple iteration and a Boolean value is used.Below is a demonstration of the same −Example Live Demomy_list = [[55], [12, 17], [25, 32, 24], [58, 36, 57, 19, 14]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list) - 1) :    if len(my_list[index + 1])

Python program to find the sum of all even and odd digits of an integer list

AmitDiwan
Updated on 04-Sep-2021 11:19:22

2K+ Views

When it is required to find the sum of all even and odd digits of an integer list, a simple iteration and the ‘modulus’ operator are used.Below is a demonstration of the same −Example Live Demomy_list = [369, 793, 2848, 4314, 57467] print("The list is :") print(my_list) sum_odd = 0 sum_even = 0 for index in my_list:    for element in str(index):       if int(element) % 2 == 0:          sum_even += int(element)       else:          sum_odd += int(element) print("The result is :") print("The sum of ... Read More

Python – Test for Word construction from character list

AmitDiwan
Updated on 04-Sep-2021 11:18:00

194 Views

When it is required to test for word construction from character list, the ‘all’ operator and the ‘count’ method is used.Below is a demonstration of the same −Example Live Demomy_list = ['p', 'p', 'y', 't', 'h', 'p', 'p', 'y', 'n', 'y', 'y', 't'] print("The list is :") print(my_list) key = 'pyt' print("The key is :") print(key) my_result = all(key.count(chr)

Python – Get Every Element from a String List except for a specified letter

AmitDiwan
Updated on 04-Sep-2021 11:16:20

412 Views

When it is required to get every element from a list of strings except a specified letter, a list comprehension and the ‘append’ method is used.Below is a demonstration of the same −Example Live Demomy_list = ["hi", "is", "great", "pyn", "pyt"] print("The list is :") print(my_list) my_key = 'n' print("The value for key is ") print(my_key) my_result = [] for sub in my_list:    my_result.append(''.join([element for element in sub if element == my_key])) print("The result is :") print(my_result)OutputThe list is : ['hi', 'is', 'great', 'pyn', 'pyt'] The value for key is n The result is ... Read More

Python - Print rows from the matrix that have same element at a given index

AmitDiwan
Updated on 04-Sep-2021 11:15:06

303 Views

When it is required to print the rows from the matrix that have the same element at the given index, a list comprehension and the ‘all’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]] print("The list is :") print(my_list) my_key = 1 print("The key is ") print(my_key) my_result = [element for element in my_list if all(str(i)[my_key] == str(element[0])[my_key] for i in element)] print("The result is :") print(my_result)OutputThe list is : [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, ... Read More

Python Program – Print the count of either peaks or valleys from a list

AmitDiwan
Updated on 04-Sep-2021 11:13:43

311 Views

When it is required to print the count of either peaks or valleys from a list, a simple iteration and a specific condition is placed.Below is a demonstration of the same −Example Live Demomy_list = [11, 12, 24, 12, 36, 17, 28, 63] print("The list is :") print(my_list) my_result = 0 for index in range(1, len(my_list) - 1):    if my_list[index + 1] > my_list[index] < my_list[index - 1] or my_list[index + 1] <    my_list[index] > my_list[index - 1]:       my_result += 1 print("The result is :") print(my_result)OutputThe list is : [11, 12, 24, ... Read More

Python – Consecutive Division in List

AmitDiwan
Updated on 04-Sep-2021 11:11:48

230 Views

When it is required to find the consecutive division in a list, a method is defined that iterates over the elements of the list and uses the ‘/’ operator to determine the result.Below is a demonstration of the same −Example Live Demodef consec_division(my_list):    my_result = my_list[0]    for idx in range(1, len(my_list)):       my_result /= my_list[idx]    return my_result my_list = [2200, 500, 100, 50, 20, 5] print("The list is :") print(my_list) my_result = consec_division(my_list) print("The result is :") print(my_result)OutputThe list is : [2200, 500, 100, 50, 20, 5] The result is ... Read More

Python – Filter immutable rows representing Dictionary Keys from Matrix

AmitDiwan
Updated on 04-Sep-2021 11:10:11

156 Views

When it is required to filter immutable rows representing dictionary keys from a matrix, a list comprehension and the ‘isinstance’ method can be used.Below is a demonstration of the same −Example Live Demomy_list = [[24, 15, [32, 33, 12]], ["pyt", 8, (14, 54)], [{15:24}, 13, "fun"], [True, "cool"]] print("The list is :") print(my_list) my_result = [row for row in my_list if all(isinstance(element, int) or isinstance(element, bool) or isinstance(element, float) or isinstance(element, tuple) or isinstance(element, str) for element in row)] print("The result is :") print(my_result)OutputThe list is : [[24, 15, [32, 33, 12]], ['pyt', 8, (14, 54)], [{15: 24}, ... Read More

Python – Extract Particular data type rows

AmitDiwan
Updated on 04-Sep-2021 11:09:15

364 Views

When it is required to extract particular data type rows, the list comprehension, the ‘isinstance’ method, and the ‘all’ operator are used.Below is a demonstration of the same −Example Live Demomy_list = [[14, 35, "Will"], [12, 26, 17], ["p", "y", "t"], [29, 40, 21]] print("The list is :") print(my_list) my_data_type = int my_result = [row for row in my_list if all(isinstance(element, my_data_type) for element in row)] print("The result is :") print(my_result)OutputThe list is : [[14, 35, 'Will'], [12, 26, 17], ['p', 'y', 't'], [29, 40, 21]] The result is : [[12, 26, 17], [29, 40, 21]]ExplanationA list ... Read More

Python – Group Consecutive elements by Sign

AmitDiwan
Updated on 04-Sep-2021 11:07:44

297 Views

When it is required to group consecutive elements by sign, the ‘^’ operator and the simple iteration along with ‘enumerate’ is used.Below is a demonstration of the same −Example Live Demomy_list = [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, 43, 30, -23, -35, 53] print("The list is :") print(my_list) my_result = [[]] for (index, element) in enumerate(my_list):    if element ^ my_list[index - 1] < 0:       my_result.append([element])    else:       my_result[-1].append(element) print("The result is :") print(my_result)OutputThe list is : [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, ... Read More

Advertisements