AmitDiwan has Published 10744 Articles

Python – Extract Sorted Strings

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:19:58

188 Views

When it is required to extract sorted strings, a list comprehension and the ‘sorted’ method is used.ExampleBelow is a demonstration of the same −my_list = ["pyt", "Fdf", "Fun"] print("The list is :") print(my_list) my_result = [element for element in my_list if ''.join(sorted(element)) == element] print("The result is ... Read More

Python – Rows with K string in Matrix

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 09:22:27

162 Views

When it is required to find rows, which have ‘K’ string in matrix, the ‘enumerate’ attribute, a simple iteration and ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = [["Pyt", "fun", "python"], ["python", "rock"], ["Pyt", "for", "CS"], ["Keep", "learning"]] print("The list is :") print(my_list) K ... Read More

Python - Merge a Matrix by the Elements of First Column

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 09:20:12

318 Views

When it is required to merge a matrix by the elements of first column, a simple iteration and list comprehension and ‘setdefault’ method is used.ExampleBelow is a demonstration of the same −my_list = [[41, "python"], [13, "pyt"], [41, "is"], [4, "always"], [3, "fun"]] print("The list is :") print(my_list) ... Read More

Python – Concatenate Strings in the Given Order

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 09:18:28

324 Views

When it is required to concatenate strings in a given order, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = ["pyt", "fun", "for", "learning"] print("The list is :") print(my_list) sort_order = [1, 0, 3, 2] my_result = '' for element in sort_order: ... Read More

Python Program to remove elements that are less than K difference away in a list

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 09:16:17

143 Views

When it is required to remove elements that are less than K difference away in a list, a simple iteration and ‘if’ condition is used.ExampleBelow is a demonstration of the same −my_list = [13, 29, 24, 18, 40, 15] print("The list is :") print(my_list) K = 3 ... Read More

Python – Append List every Nth index

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 09:13:57

585 Views

When it is required to append list every ‘N’th index, a simple iteration and the ‘enumerate’ attribute are used.ExampleBelow is a demonstration of the same −my_list = [13, 27, 48, 12, 21, 45, 28, 19, 63] print("The list is :") print(my_list) append_list = ['P', 'Y', 'T'] N ... Read More

Python – Filter Tuples Product greater than K

AmitDiwan

AmitDiwan

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

180 Views

When it is required to filter tuples product greater than K, a list comprehension is used.ExampleBelow is a demonstration of the same −def tuples_product(index):    my_result = 1    for element in index:       my_result *= element    return my_result my_list = [(14, 25, 17), (2, 3, ... Read More

Python – Adjacent elements in List

AmitDiwan

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: ... Read More

Python – Sort by Units Digit in a List

AmitDiwan

AmitDiwan

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

329 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 ... Read More

Python – Remove rows with Numbers

AmitDiwan

AmitDiwan

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

290 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 ... Read More

Advertisements