Programming Articles - Page 1081 of 3363

Python – Filter all uppercase characters from given list of tuples

AmitDiwan
Updated on 13-Sep-2021 11:26:19

676 Views

When it is required to filter all the uppercase characters from a list of tuples, a simple iteration, a Boolean value, the ‘append’ method and the ‘isupper’ methods are used.ExampleBelow is a demonstration of the same −my_list = [("PYTHON", "IS", "Fun"), ("PYTHON", "COOl"), ("PYTHON", ), "ORIENTED", "OBJECT"] print("The list is : " ) print(my_list) my_result_list = [] for sub_list in my_list:    my_result = True    for element in sub_list:       if not element.isupper():          my_result = False          break    if my_result:       my_result_list.append(sub_list) ... Read More

Python program to remove elements at Indices in List

AmitDiwan
Updated on 13-Sep-2021 11:24:11

256 Views

When it is required to remove the elements at indices in a list, the ‘enumerate’ attribute, the ‘not in’ operator, a simple iteration and the ‘append’ methods are used.ExampleBelow is a demonstration of the same −my_list = [91, 75, 15, 45, 69, 78, 23, 71, 36, 72] print("The list is : " ) print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) index_list = [2, 4, 5, 7] print("The index values stored in the list are :") print(index_list) my_result = [] for index, element in enumerate(my_list): if index not in index_list: ... Read More

Python – Filter tuple with all same elements

AmitDiwan
Updated on 13-Sep-2021 11:22:26

258 Views

When it is required to filter out the tuple that contains same elements, a list comprehension and the ‘set’ operator and the ‘len’ methods can be used.ExampleBelow is a demonstration of the same −my_list = [(31, 54, 45, 11, 99) , (11, 11), (45, 45, 45), (31, 54, 45, 11, 99), (99, 99), (0, 0)] print("The list is : " ) print(my_list) my_result = [sub_list for sub_list in my_list if len(set(sub_list)) == 1] print("The resultant list is : ") print(my_result)OutputThe list is : [(31, 54, 45, 11, 99), (11, 11), (45, 45, 45), (31, 54, 45, 11, ... Read More

Python – Convert Rear column of a Multi-sized Matrix

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

116 Views

When it is required to convert the rear column of a multi-sized matrix, a simple iteration and the ‘append’ method along with negative indexing is used.ExampleBelow is a demonstration of the same −my_list = [[41, 65, 25], [45, 89], [12, 65, 75, 36, 58], [49, 12, 36, 98], [47, 69, 78]] print("The list is : " ) print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) my_result = [] for sub_list in my_list: my_result.append(sub_list[-1]) print("The resultant list is : ") print(my_result) print("The list after sorting is : " ) ... Read More

Python – Maximum of K element in other list

AmitDiwan
Updated on 13-Sep-2021 11:17:28

221 Views

When it is required to get the maximum of K elements based on another list, a simple iteration, the ‘append’ method and the ‘max’ methods are used.ExampleBelow is a demonstration of the same −my_list_1 = [62, 25, 32, 98, 75, 12, 46, 53] my_list_2 = [91, 42, 48, 76, 23, 17, 42, 83] print("The first list is : " ) print(my_list_1) print("The first list after sorting is : " ) my_list_1.sort() print(my_list_1) print("The second list is : " ) print(my_list_2) print("The first list after sorting is : " ) my_list_2.sort() print(my_list_2) K = 42 print("The ... Read More

Python – Split Numeric String into K digit integers

AmitDiwan
Updated on 13-Sep-2021 11:15:05

390 Views

When it is required to split a numeric string into K digit integers, a simple iteration, the ‘int’ method and the ‘append’ methods are used.ExampleBelow is a demonstration of the same −my_string = '69426874124863145' print("The string is : " ) print(my_string) K = 4 print("The value of K is ") print(K) my_result = [] for index in range(0, len(my_string), K): my_result.append(int(my_string[index : index + K])) print("The resultant list is : ") print(my_result) print("The resultant list after sorting is : ") my_result.sort() print(my_result)OutputThe string is : 69426874124863145 The value of K is 4 ... Read More

Python – Check if elements in a specific index are equal for list elements

AmitDiwan
Updated on 13-Sep-2021 11:13:09

830 Views

When it is required to check if the elements in a specific index are equal to another list of elements, a simple iteration and a Boolean value are used.ExampleBelow is a demonstration of the same −my_list_1 = [69, 96, 23, 57, 13, 75, 13] my_list_2 = [68, 21, 69, 23, 49, 35, 73] print("The first list is : " ) print(my_list_1) print("The first list after sorting is :") my_list_1.sort() print(my_list_1) print("The second list is : " ) print(my_list_2) print("The first list after sorting is :") my_list_2.sort() print(my_list_2) check_list = [66, 89, 69] print("The second list ... Read More

Python – Strings with all given List characters

AmitDiwan
Updated on 13-Sep-2021 11:08:40

397 Views

When it is required to find the strings which have all the given characters present in a list, a method is defined that takes the string as a parameter and iterates through the string, and adds the index value to it.ExampleBelow is a demonstration of the same −print("Method definition begins...") def convert_to_my_string(my_string):    my_result = ""    for index in my_string:       my_result += index    return my_result print("Method definition ends...") my_string = ['L', 'e', 'a', 'r', 'n', 'P', 'y', 't', 'h', 'o', 'n', 'c', 'o', 'o', 'l', 'f', 'u', 'n'] print("The list is : " ... Read More

Python – Filter consecutive elements Tuples

AmitDiwan
Updated on 13-Sep-2021 11:06:53

307 Views

When it is required to filter consecutive elements from a list of tuple, a method is defined that takes a list of tuple as a parameter and checks the index of every tuple, and returns a Boolean value depending on the index.ExampleBelow is a demonstration of the same −print("Method definition begins...") def check_consec_tuple_elem(my_tuple):    for idx in range(len(my_tuple) - 1):       if my_tuple[idx + 1] != my_tuple[idx] + 1:          return False    return True print("Method definition ends...") my_tuple = [(23, 24, 25, 26), (65, 66, 78, 29), (11, 28, 39), (60, 61, 62, 63)] ... Read More

Python – Remove Rows for similar Kth column element

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

141 Views

When it is required to remove rows for a similar ‘K’th column element, a simple iteration, and the ‘append’ method are used.ExampleBelow is a demonstration of the same −my_list = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [] my_mem = [] for index in my_list:    if not index[K] in my_mem:       my_result.append(index)       my_mem.append(index[K]) print("The resultant list is : ") ... Read More

Advertisements