Python Articles

Page 113 of 855

Program to check whether we can make group of two partitions with equal sum or not in Python?

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

Suppose we have a list of numbers called nums, we have to check whether we can partition nums into two groups where the sum of the elements in both groups are same.So, if the input is like nums = [2, 3, 6, 5], then the output will be True, as we can make groups like: [2, 6] and [3, 5].To solve this, we will follow these stepstotal := sum of all elements in numsif total is odd, thenreturn Falsehalf := integer part of total / 2dp := a list of size half + 1 and fill with falsedp[0] := truefor ...

Read More

Program to count number of operations required to convert all values into same in Python?

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

Given a list of integers nums, you can perform the following operation: pick the largest number in nums and turn it into the second largest number. Return the minimum number of operations required to make all integers the same in the list.So, if the input is like nums = [5, 9, 2], then the output will be 3, as pick 9 first, then make it 5, so array is [5, 5, 2], then pick 5 and make 2, [5, 2, 2], again pick 5 and convert into 2, [2, 2, 2].To solve this, we will follow these stepsvals := sort ...

Read More

Program to perform excel spreadsheet operation in Python?

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

Suppose we have a 2D matrix representing an excel spreadsheet. We have to find the same matrix with all cells and formulas computed. An excel spreadsheet looks like belowB17035=A1+A2The columns are named as (A, B, C...) and rows are (1, 2, 3....) Each cell will either contain a value, a reference to another cell, or an excel formula for an operation with between numbers or cell reference. (Example. "=A1+5", "=A2+B2", or "=2+5")So, if the input is likeB17035=A1+A2then the output will be7703510as the B1 = 7 (The first row second column) and "=A1 + A2" is 7 + 3 = 10.To ...

Read More

Program to find airports in correct order in Python?

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

Suppose we have a list of flights as [origin, destination] pairs. The list is shuffled; we have to find all the airports that were visited in the correct order. If there are more than one valid itinerary, return lexicographically smallest ones first.So, if the input is like flights = [["Mumbai", "Kolkata"], ["Delhi", "Mumbai"], ["Kolkata", "Delhi"] ], then the output will be ['Delhi', 'Mumbai', 'Kolkata', 'Delhi']To solve this, we will follow these stepsins := an empty mapouts := an empty mapadj_list := an empty mapDefine a function dfs() . This will take airportwhile outs[airport] is not null, donxt := size of ...

Read More

Python - List Initialization with alternate 0s and 1s

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 1K+ Views

In this article, we are going to learn how to initialize a list with alternate 0s and 1s. We'll have list length and need to initialize with alternate 0s and 1s.Follow the below steps to initialize a list with alternate 0s and 1s.Initialize an empty list and length.Iterate length times and append 0s and 1s alternatively based on the index.Print the result.ExampleLet's see the code.# initialzing an empty list result = [] length = 7 # iterating for i in range(length):    # checking the index    if i % 2 == 0:       # appending 1 ...

Read More

List expansion by K in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 257 Views

In this article, we are going to learn how to expand the list by replicating the element K times. We'll two different ways to solve the problem.Follow the below steps to solve the problem.Initialize the list, K, and an empty list.3Iterate over the list and add current element K times using replication operator.Print the result.ExampleLet's see the code.# initializing the list numbers = [1, 2, 3] K = 5 # empty list result = [] # expanding the list for i in numbers:    result += [i] * K # printing the list print(result)If you run the ...

Read More

List consisting of all the alternate elements in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 3K+ Views

In this article, we are going to learn how to get alternate elements from the list. We'll see two different ways to solve the problem.Follow the below steps to solve the problem in one way.Initialize the list.3Iterate over the list and store all the elements from the odd index.Print the result.ExampleLet's see the code.# Initializing the list numbers = [1, 2, 3, 4, 5] # finding alternate elements result = [numbers[i] for i in range(len(numbers)) if i % 2 != 0] # printing the result print(result)If you run the above code, then you will get the following result.[2, ...

Read More

Last occurrence of some element in a list in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 11K+ Views

In this article, we are going to see different ways to find the last occurrence of an element in a list.Let's see how to find the last occurrence of an element by reversing the given list. Follow the below steps to write the code.Initialize the list.Reverse the list using reverse method.Find the index of the element using index method.The actual index of the element is len(list) - index - 1.Print the final index.ExampleLet's see the code.# initializing the list words = ['eat', 'sleep', 'drink', 'sleep', 'drink', 'sleep', 'go', 'come'] element = 'sleep' # reversing the list words.reverse() # ...

Read More

Python - Largest number possible from list of given numbers

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 2K+ Views

In this article, we are going to learn how to find the possible largest number from the given list of numbers. We will see two different ways to find to solve the problem. Follow the below steps to solve the problem.Import the itertools module for permutations method.Initialize the list with numbers and an empty list.Iterate over the permutations of the list.Join all the combinations and add the result to the empty list.Find the max number from the result with max method and key as int.Convert the string to integer and print it.ExampleLet's see the code.# importing the module import itertools ...

Read More

Python - Joining unicode list elements

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 479 Views

In this article, we are going to learn how to join Unicode list elements. Follow the below steps to write the code.Initialize the list.Convert all the elements into Unicode using map and string.encode methods.Convert each encoded string using the decode method.Join the strings using join method.Print the result.Example# initializing the list strings = ['Tutorialspoint', 'is a popular', 'site', 'for tech leranings'] def get_unicode(string): return string.encode() # converting to unicode strings_unicode = map(get_unicode, strings) # joining the unicodes result = ' '.join(unicode.decode() for unicode in strings_unicode) # printing the result print(result)If you run the above code, then ...

Read More
Showing 1121–1130 of 8,547 articles
« Prev 1 111 112 113 114 115 855 Next »
Advertisements