Python Articles

Page 10 of 854

Python – Sort by Maximum digit in Element

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 241 Views

When it is required to sort by maximum digit in element, a method is defined that uses ‘str’ and ‘max’ method to determine the result.Below is a demonstration of the same −Exampledef max_digits(element):    return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") print(my_list) my_list.sort(key = max_digits) print("The result is :") print(my_list)OutputThe list is : [224, 192, 145, 18, 3721] The result is : [224, 145, 3721, 18, 192]ExplanationA method named ‘max_digits’ is defined that takes element as a parameter, and converts it into a string, and then gets the maximum of ...

Read More

Python program to sort matrix based upon sum of rows

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 286 Views

When it is required to sort matrix based upon sum of rows, a method is defined that uses ‘sum’ method to determine the result.Below is a demonstration of the same −Exampledef sort_sum(row):    return sum(row) my_list = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print("The list is :") print(my_list) my_list.sort(key = sort_sum) print("The result is :") print(my_list)OutputThe list is : [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] The result is : [[12, 41], [34, 51], [54, 36, 22], [32, 15, 67]]ExplanationA method named ‘sort_sum’ is defined that takes a list ...

Read More

Python – Combine list with other list elements

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 238 Views

When it is required to combine list with other list elements, a simple iteration and ‘append’ method is used.Below is a demonstration of the same −Examplemy_list_1 = [12, 14, 25, 36, 15] print("The first list is :") print(my_list_1) my_list_2 = [23, 15, 47, 12, 25] print("The second list is :") print(my_list_2) for element in my_list_2 :    my_list_1.append(element) print ("The result is :") print(my_list_1)OutputThe first list is : [12, 14, 25, 36, 15] The second list is : [23, 15, 47, 12, 25] The result is : [12, 14, 25, 36, 15, 23, 15, 47, ...

Read More

Python – Extract Rear K digits from Numbers

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 262 Views

When it is required to extract rear K digits from numbers, a simple list comprehension, the modulo operator and the ‘**’ operator are used.Below is a demonstration of the same −Examplemy_list = [51645, 24567, 36743, 89452, 2122] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) my_result = [element % (10 ** K) for element in my_list] print("The result is :") print(my_result)OutputThe list is : [51645, 24567, 36743, 89452, 2122] The value of K is 3 The result is : [645, 567, 743, 452, 122]ExplanationA list is defined and displayed ...

Read More

Python – Dual Tuple Alternate summation

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 223 Views

When it is required to perform dual tuple alternate summation, a simple iteration and the modulus operator are used.Below is a demonstration of the same −Examplemy_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print("The list is :") print(my_list) my_result = 0 for index in range(len(my_list)):    if index % 2 == 0:       my_result += my_list[index][0]    else:       my_result += my_list[index][1] print("The result is :") print(my_result)OutputThe list is : [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] The result is : 225ExplanationA list of tuple ...

Read More

Python Program to extracts elements from a list with digits in increasing order

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 202 Views

When it is required to extracts elements from a list with digits in increasing order, a simple iteration, a flag value and the ‘str’ method is used.Below is a demonstration of the same −Examplemy_list = [4578, 7327, 113, 3467, 1858] print("The list is :") print(my_list) my_result = [] for element in my_list:    my_flag = True    for index in range(len(str(element)) - 1):       if str(element)[index + 1]

Read More

Python Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

When it is required to remove a specific occurrence of a given word in a list of words, given that the words can be repeated, a method can be defined, that iterates through the list, and increments the counter by 1. If the count and the specific occurrence match, then the specific element from the list can be deleted.Below is a demonstration of the same −Exampledef remove_word(my_list, my_word, N):    count = 0    for i in range(0, len(my_list)):       if (my_list[i] == my_word):       count = count + 1       if(count ...

Read More

Python Program to Find Element Occurring Odd Number of Times in a List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

When it is required to find an element that occurs odd number of times in a list, a method can be defined. This method iterates through the list and checks to see if the elements in the nested loops match. If they do, the counter is incremented. If that count is not divisible by 2, the specific element of the list is returned as the result. Otherwise, -1 is returned as the result.Below is a demonstration of the same −Exampledef odd_occurence(my_list, list_size):    for i in range(0, list_size):       count = 0       for j ...

Read More

Python Program to Replace all Occurrences of ‘a’ with $ in a String

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 831 Views

When it is required to replace all the occurrences of ‘a’ with a character such as ‘$’ in a string, the string can be iterated over and can be replaced using the ‘+=’ operator.Below is a demonstration of the same −Examplemy_str = "Jane Will Rob Harry Fanch Dave Nancy" changed_str = '' for char in range(0, len(my_str)):    if(my_str[char] == 'a'):       changed_str += '$'    else:       changed_str += my_str[char] print("The original string is :") print(my_str) print("The modified string is : ") print(changed_str)OutputThe original string is : Jane Will Rob Harry Fanch Dave ...

Read More

Python Program to Take in a String and Replace Every Blank Space with Hyphen

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

When it is required to take a string and replace every blank space with a hyphen, the ‘replace’ method can be used. It takes two parameters, the blank space, and the value with which it needs to be replaced (hyphen in this case).Below is a demonstration of the same −Examplemy_string = input("Enter a string :") print("The string entered by user is :") print(my_string) my_string = my_string.replace(' ', '-') print("The modified string:") print(my_string)OutputEnter a string : A-B-C-D E- A-B-C-D E- The string entered by user is : A-B-C-D E- The modified string: A-B-C-D--E-ExplanationAn input string is asked to be entered by ...

Read More
Showing 91–100 of 8,532 articles
« Prev 1 8 9 10 11 12 854 Next »
Advertisements