Found 33676 Articles for Programming

Python – Adjacent elements in List

AmitDiwan
Updated on 08-Sep-2021 09:10:04

2K+ Views

When it is required to display adjacent elements in list, a method is defined that uses enumerate and a simple iteration to determine the result.ExampleBelow is a demonstration of the same −def find_adjacent_elements(my_list):    my_result = []    for index, ele in enumerate(my_list):       if index == 0:          my_result.append((None, my_list[index + 1]))       elif index == len(my_list) - 1:          my_result.append((my_list[index - 1], None))       else:          my_result.append((my_list[index - 1], my_list[index + 1]))    return my_result my_list = [13, 37, 58, 12, 41, ... Read More

Python – Sort by Units Digit in a List

AmitDiwan
Updated on 08-Sep-2021 09:08:08

331 Views

When it is required to sort by units digit in a list, a method is defined that takes one parameter and uses ‘str’ and negative indexing to determine the output.ExampleBelow is a demonstration of the same −def unit_sort(element):    return str(element)[-1] my_list = [716, 134, 343, 24742] print("The list is :") print(my_list) my_list.sort(key=unit_sort) print("The result is :") print(my_list)OutputThe list is : [716, 134, 343, 24742] The result is : [24742, 343, 134, 716]ExplanationA method named ‘unit_sort’ is defined that takes an element of list as a parameter, and returns the last element after converting it to ... Read More

Python – Remove rows with Numbers

AmitDiwan
Updated on 08-Sep-2021 09:05:17

294 Views

When it is required to remove rows with numbers, a list comprehension and the ‘not’ and ‘any’ operators are used.ExampleBelow is a demonstration of the same −my_list = [[14, 'Pyt', 'fun'], ['Pyt', 'is', 'best'], [23, 51], ['Pyt', 'fun']] print("The list is :") print(my_list) my_result = [index for index in my_list if not any(isinstance(element, int) for element in index)] print("The result is :") print(my_result)OutputThe list is : [[14, 'Pyt', 'fun'], ['Pyt', 'is', 'best'], [23, 51], ['Pyt', 'fun']] The result is : [['Pyt', 'is', 'best'], ['Pyt', 'fun']]ExplanationA list of list is defined and displayed on the console.A list comprehension ... Read More

Python - Sort Matrix by Maximum Row element

AmitDiwan
Updated on 08-Sep-2021 09:03:19

200 Views

When it is required to sort matrix by maximum row element, a method is defined that takes one parameter and uses ‘max’ method to determine the result.ExampleBelow is a demonstration of the same −def sort_max(row):    return max(row) my_list = [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] print("The list is :") print(my_list) my_list.sort(key = sort_max, reverse = True) print("The result is :") print(my_list)OutputThe list is : [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] The result is : [[13, 15, 56], [43, 13, 25], [39, 20, 13], ... Read More

Python – Display the key of list value with maximum range

AmitDiwan
Updated on 08-Sep-2021 09:01:00

199 Views

When it is required to display the key of list value with maximum range, a simple iteration is used.ExampleBelow is a demonstration of the same −my_dict = {"pyt" : [26, 12, 34, 21], "fun" : [41, 27, 43, 53, 18], "learning" : [21, 30, 29, 13]} print("The dictionary is :") print(my_dict) max_result = 0 for sub, values in my_dict.items():    max_result = max(max_result, max(values) - min(values))    if max_result == max(values) - min(values):       result = sub print("The result is :") print(result)OutputThe dictionary is : {'pyt': [26, 12, 34, 21], 'fun': [41, 27, 43, ... Read More

Python – Find occurrences for each value of a particular key

AmitDiwan
Updated on 08-Sep-2021 08:55:11

445 Views

When it is required to find the occurrences for each value of a particular key, a list comprehension and the lambda method is used.Below is a demonstration of the same −Examplefrom itertools import groupby my_dict = [{'pyt' : 13, 'fun' : 44}, {'pyt' : 63, 'best' : 15}, {'pyt' : 24, 'fun' : 34}, {'pyt' : 47, 'best' : 64} ] print("The dictionary is :") print(my_dict) my_key = 'pyt' print("The key value is :") print(my_key) my_result = [{keys: len(list(value))} for keys, value in groupby(my_dict, lambda index: index[my_key])] print("The result is :") print(my_result)OutputThe dictionary is : [{'pyt': ... Read More

Python program to extract Mono-digit elements

AmitDiwan
Updated on 08-Sep-2021 08:51:53

310 Views

When it is required to extract mono-digit elements, list comprehension and the ‘all operator are used.Below is a demonstration of the same −Examplemy_list = [863, 1, 463, "pyt", 782, 241, "is", 639, 4, "fun"] print("The list is :") print(my_list) my_result = [index for index in my_list if all(str(element) == str(index)[0] for element in str(index))] print("The result is :") print(my_result)OutputThe list is : [863, 1, 463, 'pyt', 782, 241, 'is', 639, 4, 'fun'] The result is : [1, 4]ExplanationA list is defined and displayed on the console.A list comprehension is used to iterate over the list, and every ... Read More

Python – Sort Dictionaries by Size

AmitDiwan
Updated on 08-Sep-2021 08:49:52

202 Views

When it is required to sort dictionaries by size, a method is defined that takes one parameter and uses ‘len’ to determine the output.Below is a demonstration of the same −Exampledef get_len(element):    return len(element) my_dict = [{24: 56, 29: 11, 10: 22, 42: 28}, {54: 73, 59: 11}, {13: 39}, {31: 22, 59: 73, 57: 44}] print("The dictionary is :") print(my_dict) my_dict.sort(key=get_len) print("The result is :") print(my_dict)OutputThe dictionary is : [{24: 56, 29: 11, 10: 22, 42: 28}, {54: 73, 59: 11}, {13: 39}, {31: 22, 59: 73, 57: 44}] The result is : [{13: ... Read More

Python – Cross Pattern Pairs in List

AmitDiwan
Updated on 08-Sep-2021 08:48:03

244 Views

When it is required to display cross pattern pairs in list, a list comprehension and the ‘*’ operator are used.Below is a demonstration of the same −Examplemy_list_1 = [14, 35, 26] my_list_2 = [36, 24, 12] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) result = [i * j for j in my_list_1 for i in my_list_2] print ("The result is :") print(result)OutputThe first list is : [14, 35, 26] The second list is : [36, 24, 12] The result is : [504, 336, 168, 1260, 840, 420, 936, 624, 312]ExplanationTwo lists are ... Read More

Python – Nearest occurrence between two elements in a List

AmitDiwan
Updated on 08-Sep-2021 08:42:29

288 Views

When it is required to display nearest occurrence between two elements in a list, a method is defined that takes three parameters. It uses the ‘not in’ operator, and the list comprehension to determine the result.Below is a demonstration of the same −Exampledef nearest_occurence_list(my_list, x, y):    if x not in my_list or y not in my_list:       return -1    x_index = [index for index in range(len(my_list)) if my_list[index] == x]    y_index = my_list.index(y)    min_dist = 1000000    result = None    for element in x_index:       if abs(element - y_index) < min_dist: ... Read More

Advertisements