Python Articles

Page 18 of 852

Python Program to Merge Two Lists and Sort it

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

When it is required to merge two lists and sort them, a method can be defined that sorts the list using ‘sort’ method.Below is the demonstration of the same −Exampledef merge_list(list_1, list_2):    merged_list = list_1 + list_2    merged_list.sort()    return(merged_list) list_1 = [20, 18, 9, 51, 48, 31] list_2 = [28, 33, 3, 22, 15, 20] print("The first list is :") print(list_1) print("The second list is :") print(list_2) print(merge_list(list_1, list_2))OutputThe first list is : [20, 18, 9, 51, 48, 31] The second list is : [28, 33, 3, 22, 15, 20] [3, 9, 15, 18, 20, 20, ...

Read More

Python Program to Find the Second Largest Number in a List Using Bubble Sort

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 980 Views

When it is required to find the second largest number in a list using bubble sort, a method named ‘bubble_sort’ is defined, that sorts the elements of the list. Once this is done, another method named ‘get_second_largest’ is defined that returns the second element from the end as output.Below is the demonstration of the same −Examplemy_list = [] my_input = int(input("Enter the number of elements...")) for i in range(1, my_input+1):    b=int(input("Enter the element..."))    my_list.append(b) for i in range(0, len(my_list)):    for j in range(0, len(my_list)-i-1):       if(my_list[j]>my_list[j+1]):          temp=my_list[j]         ...

Read More

Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number

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

When it is required to create a list of tuple, and have the first element as the number, and the second element as the square of the element, list comprehension can be used.Below is the demonstration of the same −Examplemy_list = [23, 42, 67, 89, 11, 32] print(“The list is “) print(my_list) print(“The resultant tuple is :”) my_result = [(val, pow(val, 2)) for val in my_list] print(my_result)OutputThe list is [23, 42, 67, 89, 11, 32] The resultant tuple is : [(23, 529), (42, 1764), (67, 4489), (89, 7921), (11, 121), (32, 1024)]ExplanationA list is defined, and is displayed on the ...

Read More

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

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

When it is required to find all numbers in a range where there are perfect square, and sum of digits in the number is less than 10, list comprehension is used.Below is the demonstration of the same −Examplelower_limit = int(input(“Enter the lower range: “)) upper_limit = int(input(“Enter the upper range: “)) my_list = [] my_list = [x for x in range(lower_limit,upper_limit+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))

Read More

Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the First i+1 Elements From The Original List

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 480 Views

When it is required to find the sum of a list where the specific element is sum of first few elements, a method is defined, that takes list as parameter. It uses list comprehension to find the cumulative sum.Below is the demonstration of the same −Exampledef cumulative_sum(my_list):    cumulative_list = []    my_length = len(my_list)    cumulative_list = [sum(my_list[0:x:1]) for x in range(0, my_length+1)]    return cumulative_list[1:] my_list = [10, 20, 25, 30, 40, 50] print("The list is :") print(my_list) print("The cumulative sum is :") print (cumulative_sum(my_list))OutputThe list is : [10, 20, 25, 30, 40, 50] The cumulative sum ...

Read More

Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 937 Views

When it is required to sort a list of tuples in increasing order based on last element of every tuple, a method is defined, that iterates over the tuple and performs a simple swap to achieve the same.Below is the demonstration of the same −Exampledef sort_tuple(my_tup):    my_len = len(my_tup)    for i in range(0, my_len):       for j in range(0, my_len-i-1):          if (my_tup[j][-1] > my_tup[j + 1][-1]):             temp = my_tup[j]             my_tup[j]= my_tup[j + 1]           ...

Read More

Program to delete n nodes after m nodes from a linked list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 350 Views

Suppose we are given a linked list that has the start node as "head", and two integer numbers m and n. We have to traverse the list and delete some nodes such as the first m nodes are kept in the list and the next n nodes after the first m nodes are deleted. We perform this until we encounter the end of the linked list. We start from the head node, and the modified linked list is to be returned.The linked list structure is given to us as −Node    value :    next : So, if the ...

Read More

Program to find Final Prices With a Special Discount in a Shop in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have an array called prices where prices[i] represents price of the ith item in a shop. There is a special offer going on, if we buy the ith item, then we will get a discount equivalent to prices[j] where j is the minimum index such that j > i and price of jth item is less or same as price of ith item (i.e. prices[j] = prices[j], thenprices[i] := prices[i] - prices[j]come out from the loopotherwise, j := j + 1return pricesExample (Python)Let us see the following implementation to get better understanding −def solve(prices):    for i in ...

Read More

Program to find running sum of 1d array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 4K+ Views

Suppose we have an array nums. The running sum of an array as rs[i] is sum of all elements from nums[0] to nums[i]. Finally return the entire running sum of nums.So, if the input is like nums = [8, 3, 6, 2, 1, 4, 5], then the output will be [8, 11, 17, 19, 20, 24, 29], becausers[0] = nums[0] rs[1] = sum of nums[0..1] = 8 + 3 = 11 rs[2] = sum of nums[0..2] = 8 + 3 + 6 = 17 and so onTo solve this, we will follow these steps −n:= size of numsrs:= [nums[0]]for i ...

Read More

Program to perform XOR operation in an array using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 905 Views

Suppose we have an integer n and another integer start. We have to create an array called nums where nums[i] = start + 2*i (i start from 0) and n is the size of nums. Then find the bitwise XOR of all elements of nums.So, if the input is like n = 6, start = 2, then the output will be 14 because the array will be like [2+2*0, 2+2*1, ... 2+2*5] = [2, 4, 6, 8, 10, 12], then XOR of each element present in the array is 14.To solve this, we will follow these steps −count := startwhile ...

Read More
Showing 171–180 of 8,519 articles
« Prev 1 16 17 18 19 20 852 Next »
Advertisements