Articles on Trending Technologies

Technical articles with clear explanations and examples

Modifying tuple contents with list in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 355 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 785 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 6K+ 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 297 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 810 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

Grouped summation of tuple list in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 331 Views

When working with lists of tuples representing key-value pairs, you often need to group them by keys and sum their values. Python's Counter from the collections module provides an elegant solution for this task. The Counter is a specialized dictionary subclass that counts hashable objects. It creates a hash table automatically when initialized with an iterable, making it perfect for aggregating values by keys. Using Counter for Grouped Summation Here's how to sum values for matching keys across multiple tuple lists ? from collections import Counter my_list_1 = [('Hi', 14), ('there', 16), ('Jane', 28)] ...

Read More

Reverse each tuple in a list of tuples in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 447 Views

When it is required to reverse each tuple in a list of tuples, the negative step slicing can be used. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list. In negative slicing, the index is accessed using negative numbers, instead of positive ones. The slice notation [::-1] reverses the order of elements in each tuple. Using List Comprehension with Negative Slicing Below is a demonstration for the same − ...

Read More

Python program to check whether the string is Symmetrical or Palindrome

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

When checking if a string is symmetrical or palindromic, we need to understand two different concepts. A palindrome reads the same forwards and backwards (like "racecar"), while a symmetrical string has identical first and second halves (like "abcabc"). Understanding Palindromes vs Symmetrical Strings A palindrome compares characters from both ends moving inward, while symmetry compares the first half with the second half of the string. Complete Example Here's a program that demonstrates both palindrome and symmetry checking ? def check_palindrome(my_str): mid_val = (len(my_str)-1)//2 start = 0 ...

Read More

Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 889 Views

When counting how many times a specific letter appears in a string, we can use recursion to break down the problem into smaller subproblems. Recursion solves the bigger problem by computing results from smaller parts and combining them together. Recursive Approach The recursive function checks each character one by one. If the current character matches our target, we count it and continue with the rest of the string ? def count_letter_recursive(text, target_letter): # Base case: empty string if not text: ...

Read More

Python Program to Determine Whether a Given Number is Even or Odd Recursively

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When determining whether a number is even or odd, we typically use the modulo operator. However, we can also solve this problem recursively by repeatedly subtracting 2 until we reach a base case. Recursion breaks down the problem into smaller subproblems. Here, we reduce the number by 2 in each recursive call until we reach 0 (even) or 1 (odd). How the Recursive Approach Works The algorithm works by: Base case: If the number is less than 2 (0 or 1), check if it's divisible by 2 Recursive case: Subtract 2 from the number and ...

Read More
Showing 5191–5200 of 61,299 articles
« Prev 1 518 519 520 521 522 6130 Next »
Advertisements