Found 26504 Articles for Server Side Programming

Python – Extract Key’s Value, if Key Present in List and Dictionary

AmitDiwan
Updated on 13-Sep-2021 11:49:16

558 Views

When it is required to extract the value of key if the key is present in the list as well as the dictionary, a simple iteration and the ‘all’ operator are used.ExampleBelow is a demonstration of the same −my_list = ["Python", "is", "fun", "to", "learn", "and", "teach", 'cool', 'object', 'oriented'] my_dictionary = {"Python" : 2, "fun" : 4, "learn" : 6} K = "Python" print("The value of K is ") print(K) print("The list is : " ) print(my_list) print("The dictionary is : " ) print(my_dictionary) my_result = None if all(K in sub for sub in ... Read More

Python – Sort Dictionary List by Key’s ith Index value

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

455 Views

When it is required to sort the list of dictionary based on the key’s ‘i’th index value, the ‘sorted’ method and the lambda methods are used.ExampleBelow is a demonstration of the same −my_list = [{"Python" : "Best", "to" : "Code"}, {"Python" : "Good", "to" : "Learn"}, {"Python" : "object", "to" : "cool"}, {"Python" : "oriented", "to" : "language"}] print("The list is : " ) print(my_list) K = "Python" print("The value of K is ") print(K) i = 2 print("The value of i is :") print(i) my_result = ... Read More

Python – Check if Splits are equal

AmitDiwan
Updated on 13-Sep-2021 11:45:31

271 Views

When it is required to check if the splits in a string are equal, the ‘len’ method, ‘list’ method and the ‘set’ operator are used along with an ‘if’ condition.ExampleBelow is a demonstration of the same −my_string = '96%96%96%96%96%96' print("The string is : " ) print(my_string) my_split_char = "%" print("The character on which the string should be split is :") print(my_split_char) my_result = len(list(set(my_string.split(my_split_char)))) == 1 print("The resultant list is : ") if(my_result == True): print("All the splits are equal") else: print("All the splits are not equal")OutputThe string is ... Read More

Python – Grouped Consecutive Range Indices of Elements

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

218 Views

When it is required to get the grouped consecutive range of indices of elements in a list, a defaultdict is created. A simple iteration, along with ‘groupby’ method, ‘len’ method, ‘list’ method and the ‘append’ methods are used.ExampleBelow is a demonstration of the same −from itertools import groupby from collections import defaultdict my_list = [63, 12, 84, 91, 52, 39, 25, 27, 20, 11, 0, 9] print("The list is : " ) print(my_list) my_index = 0 my_result = defaultdict(list) for key, sub in groupby(my_list): element = len(list(sub)) my_result[key].append((my_index, my_index + ... Read More

Python – Split Strings on Prefix Occurrence

AmitDiwan
Updated on 13-Sep-2021 11:39:00

428 Views

When it is required to split the strings based on the occurrence of the prefix, two empty lists are defined, and a prefix value is defined. A simple iteration is used along with ‘append’ method.ExampleBelow is a demonstration of the same −from itertools import zip_longest my_list = ["hi", 'hello', 'there', "python", "object", "oriented", "object", "cool", "language", 'py', 'extension', 'bjarne'] print("The list is : " ) print(my_list) my_prefix = "python" print("The prefix is :") print(my_prefix) my_result, my_temp_val = [], [] for x, y in zip_longest(my_list, my_list[1:]): my_temp_val.append(x) if y and y.startswith(my_prefix): ... Read More

Python – Extract Percentages from String

AmitDiwan
Updated on 13-Sep-2021 11:27:44

439 Views

When it is required to extract the percentages from a string, the regular expressions and the ‘findall’ method of the package is used.ExampleBelow is a demonstration of the same −import re my_string = 'Python is % always fun % to learn % and teach' print("The list is : " ) print(my_string) my_result = re.findall('\d*%', my_string) print("The resultant list is : ") print(my_result)OutputThe list is : Python is % always fun % to learn % and teach The resultant list is : ['%', '%', '%']ExplanationThe required packages are imported into the environment.A string is defined and is ... Read More

Python – Filter all uppercase characters from given list of tuples

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

645 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

235 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

222 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

100 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

Advertisements