Server Side Programming Articles

Page 413 of 2109

Remove tuple from list of tuples if not containing any character in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 565 Views

When working with lists of tuples, you may need to remove tuples that don't contain any alphabetic characters. This can be achieved using list comprehension combined with the any() function and isalpha() method. A list of tuples contains tuples enclosed in a list, where each tuple can hold heterogeneous data types. List comprehension provides a concise way to filter and transform data based on specific conditions. Syntax filtered_list = [(a, b) for a, b in original_list if any(c.isalpha() for c in a)] Example Let's remove tuples whose first element doesn't contain any alphabetic ...

Read More

Remove tuples from list of tuples if greater than n in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 481 Views

When working with lists of tuples, you may need to remove tuples based on a numeric condition. This can be accomplished using list comprehension, the filter() function, or lambda expressions to check if tuple values are greater than a threshold n. A lambda function is an anonymous function defined without a name using the lambda keyword. It takes any number of arguments but contains only a single expression, making it perfect for simple filtering operations. Using List Comprehension The most Pythonic way is using list comprehension to filter tuples ? my_tuples = [('a', 130), ('b', ...

Read More

Update a list of tuples using another list in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 376 Views

When you need to update a list of tuples using another list, the defaultdict from the collections module provides an elegant solution. This approach allows you to merge tuples with matching keys and aggregate their values. defaultdict is a container similar to dictionaries that automatically creates missing keys with a default value. It's a subclass of the dict class that never raises a KeyError, making it perfect for grouping operations. Example Here's how to merge two lists of tuples and keep the maximum value for each key ? from collections import defaultdict def merge_vals(list_1, ...

Read More

Selective value selection in list of tuples in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 199 Views

When you need to extract specific values from a list of tuples based on certain keys, Python provides an elegant solution using the dict() method combined with list comprehension and the get() method. A list of tuples contains tuples enclosed in a list, often representing key-value pairs. The dict() method can convert this structure into a dictionary for efficient lookups. The get() method safely retrieves values from a dictionary, returning a default value if the key doesn't exist. Basic Example Here's how to select specific values from a list of tuples ? # List of ...

Read More

Split tuple into groups of n in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 647 Views

When it is required to split a tuple into groups of 'n' elements, list comprehension can be used along with slicing. A tuple is an immutable data type, meaning values once defined cannot be changed by accessing their index elements. If we try to change the elements, it results in an error. Tuples are important containers since they ensure read-only access. List comprehension is a shorthand to iterate through sequences and perform operations on them efficiently. Method 1: Using List Comprehension with Slicing The most common approach uses list comprehension with range() and slicing ? ...

Read More

Modifying tuple contents with list in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 302 Views

When it is required to modify the contents of tuples within a list, the zip() method combined with list comprehension provides an efficient solution. The zip() method takes iterables, aggregates them element-wise into tuples, and returns an iterator. List comprehension provides a concise way to iterate through lists and perform operations on them. A list can store heterogeneous values (data of any type like integers, strings, floats, etc.). When working with a list of tuples, you often need to modify specific elements within each tuple using values from another list. Example Here's how to modify the ...

Read More

Remove tuples having duplicate first value from given list of tuples in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 695 Views

When removing tuples with duplicate first values from a list of tuples, you can use a set to track visited first elements. This approach preserves the first occurrence of each unique first value. Using Set to Track First Values The most efficient approach uses a set to remember which first values have been seen ? tuples_list = [(45.324, 'Hi Jane, how are you'), (34252.85832, 'Hope you are good'), ...

Read More

Python Program to Implement Binary Search without Recursion

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 25-Mar-2026 5K+ Views

To implement binary search without recursion, the program uses an iterative approach with a while loop. Binary search is an efficient algorithm that finds a specific element in a sorted list by repeatedly dividing the search space in half. This method compares the target with the middle element of the current search range. If they match, the search is complete. If the target is smaller, it searches the left half; if larger, it searches the right half. This process continues until the element is found or the search space is exhausted. Algorithm Steps The iterative binary search ...

Read More

Sort tuple based on occurrence of first element in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 257 Views

When you need to sort tuples based on the occurrence frequency of their first element, Python provides several approaches. This technique is useful for grouping and counting elements in data analysis tasks. Understanding the Problem Given a list of tuples, we want to group them by the first element and count how many times each first element appears. The result includes the first element, associated values, and occurrence count. Using Dictionary with setdefault() The setdefault() method creates a dictionary entry if the key doesn't exist, making it perfect for grouping ? def sort_on_occurrence(my_list): ...

Read More

Removing strings from tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 750 Views

When it is required to remove strings from a tuple, the list comprehension and the type() method can be used. This approach allows you to filter out string elements while preserving other data types like integers and floats. A list can be used to store heterogeneous values (data of any data type like integer, floating point, strings, and so on). A list of tuples contains tuples enclosed in a list. List comprehension is a shorthand to iterate through the list and perform operations on it. The type() method returns the class of the object passed to it as ...

Read More
Showing 4121–4130 of 21,090 articles
« Prev 1 411 412 413 414 415 2109 Next »
Advertisements