Server Side Programming Articles

Page 338 of 2109

Python – Filter consecutive elements Tuples

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 345 Views

When working with tuples in Python, you may need to filter out only those tuples that contain consecutive elements. This involves checking if each element in a tuple is exactly one more than the previous element. Understanding Consecutive Elements Consecutive elements are numbers that follow each other in sequence with a difference of 1. For example, (23, 24, 25, 26) contains consecutive elements, while (65, 66, 78, 29) does not because 78 is not 67. Method to Check Consecutive Elements We can create a function that iterates through a tuple and compares each element with the ...

Read More

Python – Remove Rows for similar Kth column element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 176 Views

When working with lists of lists in Python, you may need to remove rows that have duplicate values in a specific column (Kth position). This can be achieved using simple iteration and the append() method to build a new list with unique values. Example Below is a demonstration of removing rows with duplicate Kth column elements − data = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] print("The list is:") print(data) K = 1 print("The value of K is:") print(K) result = [] ...

Read More

Python – Count frequency of sublist in given list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 526 Views

When it is required to count the frequency of a sub−list in a given list, a list comprehension and the 'len' method along with the 'if' condition are used. Example Below is a demonstration of the same − my_list = [23, 33, 45, 67, 54, 43, 33, 45, 67, 83, 33, 45, 67, 90, 0] print("The list is:") print(my_list) sub_list = [33, 45, 67] print("The sub-list is:") print(sub_list) my_result = len([sub_list for index in range(len(my_list)) if my_list[index : index + len(sub_list)] == sub_list]) print("The frequency count is:") print(my_result) The ...

Read More

Python – Segregate elements by delimiter

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 252 Views

When we need to segregate elements based on a delimiter, Python provides several approaches using split() method with list comprehensions or loops. This is commonly used for processing structured data like CSV-style strings or log entries. Basic Approach Using List Comprehension The most efficient way is to use list comprehensions with the split() method ? data_list = ["89@21", "58@51", "19@61", "11@10", "32@65", "34@45", "87@90", "32@21", "1@345"] print("The original list is:") print(data_list) delimiter = "@" print(f"Using delimiter: {delimiter}") # Segregate elements before and after delimiter before_delim = [item.split(delimiter)[0] for item in data_list] after_delim = [item.split(delimiter)[1] ...

Read More

Python – Extract elements in between multiple specific range of index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 782 Views

When you need to extract elements from multiple specific index ranges in a list, you can use the extend() method with list slicing and iteration. This technique allows you to collect elements from different sections of a list based on defined ranges. Basic Example Here's how to extract elements from multiple index ranges ? my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] print("Original list:") print(my_list) # Define multiple ranges as (start, end) tuples range_list = [(2, 4), (7, 8), (1, 2)] # Extract elements from specified ranges result ...

Read More

Python – Sort List items on basis of their Digits

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 452 Views

When sorting a list based on digit patterns rather than numerical value, we use string manipulation techniques. The sorted() method with lambda function and ljust() helps achieve lexicographic sorting of numbers. Example Below is a demonstration of sorting numbers based on their digit patterns ? my_list = [4344, 2611, 122, 541, 33, 892, 48292, 460, 390, 120, 10, 2909, 11239, 1] print("The list is:") print(my_list) print("The list after normal sorting is:") my_list_copy = sorted(my_list) print(my_list_copy) # Convert to strings to find max length my_temp_val = list(map(str, my_list)) my_max_length = max(map(len, my_temp_val)) # ...

Read More

Python – Random insertion of elements K times

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 358 Views

When it is required to randomly insert elements K times, the random package provides methods like randint() and choice() to select random positions and elements for insertion. Example Below is a demonstration of the same − import random my_list = [34, 12, 21, 56, 8, 9, 0, 3, 41, 11, 90] print("The list is : ") print(my_list) print("The list after sorting is : ") my_list.sort() print(my_list) to_add_list = ["Python", "Object", "oriented", "language", "cool"] K = 3 print("The value of K is") print(K) for element in range(K): ...

Read More

Python program to find all the Combinations in a list with the given condition

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 688 Views

When you need to find combinations in a list with a specific condition, you can use iteration, the yield operator, and list comprehension. This approach is useful for merging two lists by taking K elements from each list alternately. Syntax The basic approach involves creating a generator function that yields elements based on a given condition ? def merge_function(list1, list2, k): # Generator function logic yield element Example Below is a demonstration that merges two lists by alternating K elements from each ? ...

Read More

Python Pandas - Query the columns of a DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 517 Views

To query the columns of a Pandas DataFrame, use the query() method. This method allows you to filter records using a string expression, making it easier to write complex conditions in a readable format. Creating a DataFrame Let's start by creating a sample DataFrame with product inventory data ? import pandas as pd dataFrame = pd.DataFrame({ "Product": ["SmartTV", "PenDrive", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("DataFrame...") print(dataFrame) DataFrame... Product ...

Read More

Python Pandas - How to select multiple rows from a DataFrame

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

To select multiple rows from a DataFrame, you can use various methods including slicing with the : operator, loc[] for label-based selection, and iloc[] for position-based selection. Creating a DataFrame First, let's create a sample DataFrame to work with ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]], index=['w', 'x', 'y', 'z'], ...

Read More
Showing 3371–3380 of 21,090 articles
« Prev 1 336 337 338 339 340 2109 Next »
Advertisements