Python Articles

Page 114 of 855

Python - Joining only adjacent words in list

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

In this article, we are going to learn how to join adjacent words in a list, not digits. Follow the below steps to solve the problem.Initialize the list.Find the words that are not digits using isalpha method.4Join the words using join method.Add all the digits at the end by finding them using the isdigit method.Print the result.Example# initialzing the list strings = ['Tutorials', '56', '45', 'point', '1', '4'] # result result = [] words = [element for element in strings if element.isalpha()] digits = [element for element in strings if element.isdigit()] # adding the elements to result ...

Read More

Python - Intersection of two String

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

In this article, we are going to learn how to intersect two strings in different ways.Follow the below the steps to solve the problem.Initialize two strings and an empty string.Iterate over the first string and add the current character to the new string if it presents in the second string as well and not present in new string already.Print the result.Example# initializing the string string_1 = 'tutorialspoint' string_2 = 'tut' result = '' # finding the common chars from both strings for char in string_1:    if char in string_2 and not char in result:       ...

Read More

Python - Intersection of multiple lists

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

In this article, we are going to see how to intersect two lists that contain multiple lists in different ways. Let's start in the traditional way.Follow the below the steps to solve the problemInitialize two lists with multiple listsIterate over the first list and add the current item in the new list if it presents in the second list as well.Print the result.Example# initializing the lists list_1 = [[1, 2], [3, 4], [5, 6]] list_2 = [[3, 4]] # finding the common items from both lists result = [sub_list for sub_list in list_1 if sub_list in list_2] # ...

Read More

Python - Intersect two dictionaries through keys

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

In this article, we are going to learn how to intersect two dictionaries using keys. We have to create a new dictionary with common keys. Let's see an example.Input: dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} Output: {'A': 1, 'C': 3}We are going to use the dictionary comprehension to solve the problem. Follow the below steps to write the code.Initialize dictionaries.Iterate over the dictionary one and add elements that are not in dictionary two.Print the result.Example# initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': ...

Read More

Python - Inserting item in sorted list maintaining order

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

In this article, we are going to learn how to insert an item in a sorted list maintaining the order. Python has a built-in module called bisect that helps us to insert any element in an appropriate position in the list.Follow the below steps to write the code.Import the module bisect.Initialize list and element that need to insertThe module bisect has a method called insort that inserts an element into a list in an appropriate position. Use the method and insert the element.Print the list.Example# importing the module import bisect # initializing the list, element numbers = [10, 23, ...

Read More

Design Log Storage System in Python

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

Suppose we have some logs, that each log contains a unique id and timestamp. The Timestamp is a string that has the format: Year:Month:Day:Hour:Minute: Second, for example, 2019:01:01:23:59:59. All domains are zero-padded decimal numbers.We have to design a log storage system to implement the following functions −void Put(int id, string timestamp): This will take the log's unique id and timestamp, and it stores the log in the storage system.int[] Retrieve(String start, String end, String granularity): This will return the id of logs whose timestamps are within the range from start to end parameters. The granularity parameter indicates the time level ...

Read More

Max Increase to Keep City Skyline in Python

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

Suppose we have a 2-dimensional array called grid, where each value of grid[i][j] represents the height of a building located there. We can increase the height of any number of buildings, by any amount. Height 0 is considered to be a building as well. At the end, the "skyline" when viewed from all four directions of the grid, must be the same as the skyline of the original grid. Because a city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. So we have to find the maximum total sum that ...

Read More

Campus Bikes II in Python

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

Suppose we have a 2D grid, that represents a campus, there are N workers and M bikes, The value of N

Read More

Program to find maximum sum by flipping each row elements in Python

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

Suppose we have a 2D binary matrix. For any row or column in the given matrix we can flip all the bits. If we can perform any number of these operations, and that we treat each row as a binary number, we have to find the largest sum that can be made of these numbers.So, if the input is like010001then the output will be 11, as if we flip both rows we get 101 and 110, then the sum is 5 + 6 = 11To solve this, we will follow these steps −for each row r in matrix, doif r[0] ...

Read More

Program to find number of friend groups in a set of friends connections in Python

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

Suppose we have a a friends list, where friends[i] is a list of people i is friends with. The connection of friendships are two-way. And each person is friend with themselves and two people are in a friend group as long as there is some path of mutual friends connecting them. We have to find the total number of friend groups.So, if the input is like friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]], then the output will be 3, as The three friend groups are as below −To solve this, we will follow ...

Read More
Showing 1131–1140 of 8,547 articles
« Prev 1 112 113 114 115 116 855 Next »
Advertisements