Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Nearest occurrence between two elements in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 344 Views

When you need to find the nearest occurrence between two elements in a list, you can create a function that calculates the minimum distance between all occurrences of the first element and the target element. This approach uses list comprehension and the abs() function to find the closest match. Example Here's how to find the index of the nearest occurrence ? def nearest_occurrence_list(my_list, x, y): if x not in my_list or y not in my_list: return -1 ...

Read More

Python – Filter Tuples with Integers

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 250 Views

When it is required to filter tuples that contain only integers, a simple iteration with the 'not' operator and the 'isinstance' method can be used to check each element's data type. Example Below is a demonstration of filtering tuples with integers − my_tuple = [(14, 25, "Python"), (5, 6), (3, ), ("cool", )] print("The tuple is :") print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub: if not isinstance(element, int): ...

Read More

Python – Extract tuples with elements in Range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 346 Views

When working with tuples in Python, you may need to extract only those tuples where all elements fall within a specific range. This can be accomplished using the filter() function combined with lambda expressions and the all() function. Syntax The general approach uses this pattern ? filtered_tuples = list(filter(lambda tuple: all(start

Read More

Python – Extract Kth element of every Nth tuple in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 397 Views

When working with lists of tuples, you may need to extract the Kth element from every Nth tuple. This can be accomplished using simple iteration with the range() function and list indexing. Understanding the Problem Given a list of tuples, we want to: Select every Nth tuple (e.g., every 3rd tuple: indices 0, 3, 6...) From each selected tuple, extract the Kth element Collect these elements into a new list Example Below is a demonstration of extracting the 1st element (K=1) from every 3rd tuple (N=3) − tuples_list = [(54, 51, ...

Read More

Python – Ordered tuples extraction

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 246 Views

When working with tuples, you might need to extract only those tuples that are already in sorted order. Python provides an elegant solution using list comprehension combined with the sorted() function and tuple comparison. What are Ordered Tuples? An ordered tuple is a tuple where elements are arranged in ascending order. For example, (13, 24, 56) is ordered, while (15, 74, 36, 22, 54) is not. Extracting Ordered Tuples The key idea is to compare each tuple with its sorted version. If they are equal, the tuple is already in order ? my_list = ...

Read More

Python – Concatenate Rear elements in Tuple List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 287 Views

When working with tuples in Python, you might need to concatenate the last (rear) elements from each tuple in a list. This can be accomplished using list comprehension with the join() method to create a single concatenated string. Example Below is a demonstration of concatenating rear elements from a tuple list − my_tuple = [(13, 42, "Will"), (48, "is a"), ("good boy", )] print("The tuple is : ") print(my_tuple) my_result = " ".join([sub[-1] for sub in my_tuple]) print("The result is : ") print(my_result) Output The tuple is : ...

Read More

Python – Find Kth Even Element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 227 Views

Finding the Kth even element in a list is a common programming task. Python provides several approaches using list comprehension, filtering, and indexing techniques. Method 1: Using List Comprehension Create a list of even elements and access the Kth element directly ? numbers = [14, 63, 28, 32, 18, 99, 13, 61] print("The list is:") print(numbers) K = 3 print("The value of K is:") print(K) # Find Kth even element (1-indexed) result = [element for element in numbers if element % 2 == 0][K-1] print("The Kth even element is:") print(result) ...

Read More

Python – Split List on next larger value

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 229 Views

When it is required to split a list based on the next larger value, a list comprehension, the iter method and the islice methods are used. This technique allows you to divide a list into chunks of specified sizes sequentially. Basic Example Below is a demonstration of splitting a list into chunks of different sizes − from itertools import islice my_list = [11, 22, 33, 34, 45, 26, 87, 11] print("The list is :") print(my_list) length_to_split = [2, 5, 3] print("The split length list is :") print(length_to_split) temp = iter(my_list) my_result = [list(islice(temp, ...

Read More

Python – Extract elements from Ranges in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 651 Views

When working with lists, you often need to extract elements from specific index ranges. Python provides several approaches to accomplish this task efficiently using slicing and list methods. Understanding Range Extraction Range extraction involves taking elements from a list based on start and end positions defined in tuples. Each tuple contains two values: the start index and the end index. Using extend() Method The extend() method adds all elements from a slice to the result list ? my_list = [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, 66, 99, 19] ...

Read More

Python – Fractional Frequency of elements in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 511 Views

When you need to find the fractional frequency of elements in a list, you can use a dictionary to track element occurrences, combined with the Counter method from the collections module. This approach shows how many times each element has appeared out of its total occurrences as you iterate through the list. Understanding Fractional Frequency Fractional frequency represents the current occurrence count of an element divided by its total occurrence count. For example, if element 14 appears twice in a list, its fractional frequencies would be "1/2" for the first occurrence and "2/2" for the second occurrence. ...

Read More
Showing 3881–3890 of 61,297 articles
« Prev 1 387 388 389 390 391 6130 Next »
Advertisements