Server Side Programming Articles

Page 879 of 2109

Find all triplets in a list with given sum in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

In a list of numbers we want to find out which three elements can join to give a certain sum. We call it a triplet. And in the list there can be many such triplets. For example, the sum 10 can be generated from numbers 1, 6, 3 as well as 1, 5, 4. In this article we will see how to find out all such triplets from a given list of numbers. Using Nested Loops with Set This approach uses nested loops with a set to efficiently find triplets. We iterate through the list, and for each ...

Read More

Find all possible substrings after deleting k characters in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 258 Views

Sometimes we need to find all possible substrings after deleting exactly k characters from a string. This means keeping n-k characters in their original order to form new substrings. Python provides multiple approaches to solve this problem efficiently. Using Loops and Recursion This approach uses recursion to generate all combinations by selecting characters at different positions. We track the start and end positions while building each substring ? def letterCombinations(s, temp, start, end, index, k): result = [] if index == k: ...

Read More

Find all distinct pairs with difference equal to k in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 462 Views

In this article, we will learn how to find all distinct pairs of numbers in a list where the absolute difference between the two numbers equals a given value k. We'll explore different approaches to solve this problem efficiently. Using Nested for Loops This approach uses two nested for loops to compare every possible pair of elements in the list. The outer loop visits each element, while the inner loop compares it with all remaining elements ? numbers = [5, 3, 7, 2, 9] k = 2 count = 0 # Check all possible pairs ...

Read More

Extracting rows using Pandas .iloc[] in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 775 Views

Pandas is a powerful Python library extensively used for data processing and analysis. The .iloc[] method allows you to select rows and columns from a DataFrame using integer-based indexing, making it perfect for positional data extraction. The iloc method uses zero-based indexing where the first row has index 0, second row has index 1, and so on. Similarly, columns are indexed starting from 0. Syntax DataFrame.iloc[row_indexer, column_indexer] Creating Sample Data Let's create a sample DataFrame to demonstrate iloc functionality ? import pandas as pd # Create sample DataFrame data = ...

Read More

Duplicate substring removal from list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 446 Views

Sometimes we may have a need to refine a given list by eliminating duplicate substrings from delimited strings. This can be achieved by using a combination of various methods available in Python's standard library like set(), split(), and list comprehensions. Using set() and split() The split() method segregates the elements for duplicate checking, and the set() method stores only unique elements from each string. This approach maintains grouping by original string ? Example # initializing list strings = ['xy-xy', 'pq-qr', 'xp-xp-xp', 'dd-ee'] print("Given list:", strings) # using set() and split() result = [set(sub.split('-')) ...

Read More

Custom Multiplication in list of lists in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

Multiplying elements in a list of lists (nested list) with corresponding elements from another list is a common operation in data analysis. Python provides several approaches to perform this custom multiplication efficiently. Using Nested Loops In this approach, we use two nested for loops. The outer loop iterates through each sublist, while the inner loop multiplies each element in the sublist with the corresponding multiplier ? nested_list = [[2, 11, 5], [3, 2, 8], [11, 9, 8]] multipliers = [5, 11, 0] # Original list print("The given list:", nested_list) print("Multiplier list:", multipliers) # Using ...

Read More

Custom length Matrix in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 242 Views

Sometimes when creating a matrix using Python, we may need to control how many times a given element is repeated in the resulting matrix. In this article, we will see how to create a matrix with a required number of elements when the elements are given as a list. Using zip() and List Comprehension We declare a list with elements to be used in the matrix. Then we declare another list which will hold the number of occurrences of the element in the matrix. Using the zip() function with list comprehension, we can create the resulting matrix ? ...

Read More

Creating DataFrame from dict of narray-lists in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 687 Views

Pandas is a widely used Python library for data processing and analysis. In this article, we will see how to create pandas DataFrame from Python dictionaries containing lists as values. Creating DataFrame from Dictionary with Lists A dictionary with lists as values can be directly converted to a DataFrame using pd.DataFrame(). Each key becomes a column name, and the corresponding list becomes the column data. Example Let's create a DataFrame from a dictionary containing exam schedule information ? import pandas as pd # Dictionary for Exam Schedule exam_schedule = { ...

Read More

Creating a Pandas dataframe column based on a given condition in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 373 Views

Pandas DataFrames allow you to add new columns based on conditions applied to existing data. This is useful for categorizing, labeling, or transforming data based on specific criteria. Creating the Base DataFrame Let's start with a simple exam schedule DataFrame ? import pandas as pd # Lists for Exam subjects and Days days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] subjects = ['Chemistry', 'Physics', 'Maths', 'English', 'Biology'] # Dictionary for Exam Schedule exam_data = {'Exam Day': days, 'Exam Subject': subjects} # ...

Read More

Create a Pandas Dataframe from a dict of equal length lists in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 368 Views

A Pandas DataFrame can be created from a dictionary where values are lists of equal length. This approach is useful when you have related data stored in separate lists and want to combine them into a tabular structure. Using Separate Lists and Dictionary In this approach, we declare lists individually and then use each list as a value for the appropriate key inside a dictionary. Finally, we apply pd.DataFrame() to convert the dictionary into a DataFrame ? Example import pandas as pd # Lists for Exam schedule days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] ...

Read More
Showing 8781–8790 of 21,090 articles
« Prev 1 877 878 879 880 881 2109 Next »
Advertisements