Programming Articles

Page 904 of 2547

Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python

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

In this article, we need to find a number from a list that occurs an odd number of times. We'll use Python's lambda function with the reduce() function to solve this problem efficiently. The key insight is using the XOR (^) operation, which has a special property: when you XOR a number with itself, the result is 0. This means numbers appearing an even number of times will cancel out, leaving only the number that appears an odd number of times. How XOR Works for This Problem Let's understand the XOR logic with a simple example: ...

Read More

Find the list elements starting with specific letter in Python

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

In this article, we will explore different methods to find all elements from a list that start with a specific letter. Python provides several approaches to achieve this efficiently. Using Index and lower() This method uses the lower() function to make the comparison case-insensitive. We access the first character of each element using index [0] and compare it with the test letter ? days = ['Mon', 'Tue', 'Wed', 'Thu'] test_letter = 'T' print("Given list:") print(days) # Using lower() and index to find elements starting with specific letter result = [item for item in days ...

Read More

Find most frequent element in a list in Python

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

In this article, we will see how to find the element which is most common in a given list. In other words, the element with the highest frequency. Python provides several approaches to solve this problem efficiently. Using max() and count() We apply the set() function to get unique elements of the list, then use count() to find the frequency of each element. Finally, we apply max() with a key function to get the element with the highest frequency. Example # Given list numbers = [45, 20, 11, 50, 17, 45, 50, 13, 45] print("Given ...

Read More

Find maximum value in each sublist in Python

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

We are given a list of lists (nested lists). Our task is to find the maximum value in each sublist and return them as a new list. Python provides several approaches to accomplish this efficiently. Using List Comprehension with max() The most Pythonic approach uses list comprehension combined with the max() function to iterate through each sublist ? data = [[10, 13, 454, 66, 44], [10, 8, 7, 23], [5, 15, 25]] # Given list print("The given list:") print(data) # Use max with list comprehension result = [max(sublist) for sublist in data] print("Maximum ...

Read More

Count tuples occurrence in list of tuples in Python

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

A list is made up of tuples as its elements. In this article we will count the number of unique tuples present in the list and their occurrences using different approaches. Using defaultdict We treat the given list as a defaultdict data container and count the elements in it using iteration. The defaultdict automatically initializes missing keys with a default value. Example import collections tuple_list = [('Mon', 'Wed'), ('Mon', ), ('Tue', ), ('Mon', 'Wed')] # Given list print("Given list:", tuple_list) res = collections.defaultdict(int) for elem in tuple_list: res[elem] ...

Read More

Count the sublists containing given element in a list in Python

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

When working with lists in Python, you often need to count how many times a specific element appears. This article demonstrates three different approaches to count occurrences of a given element in a list. Using range() and len() This method uses a loop with range() and len() to iterate through the list indices. A counter variable tracks how many times the element is found ? days = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] target = 'Mon' print("Given list:", days) print("Element to check:", target) count = 0 for i in range(len(days)): if ...

Read More

Count the Number of matching characters in a pair of string in Python

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

We are given two strings and need to find the count of characters in the first string that are also present in the second string. Python provides several approaches to solve this problem efficiently. Using Set Intersection The set function gives us unique values of all elements in a string. We use the & operator to find common elements between the two strings ? Example strA = 'Tutorials Point' uniq_strA = set(strA) print("Given String:", strA) strB = 'aeio' uniq_strB = set(strB) print("Search character strings:", strB) common_chars = uniq_strA & uniq_strB print("Count of matching ...

Read More

Count of elements matching particular condition in Python

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

In this article, we will see how to count elements in a Python list that match a particular condition. We'll explore different approaches to filter and count elements based on specific criteria. Using Generator Expression with sum() This approach uses a generator expression with an if condition inside the sum() function. The expression generates 1 for each element that matches the condition ? Example days = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] # Given list print("Given list:") print(days) # Count elements that are 'Mon' or 'Wed' count = sum(1 for day in days if ...

Read More

Count occurrences of an element in a list in Python

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

In this article we are given a list and a string. We are required to find how many times the given string is present as an element in the list. Python provides several methods to count occurrences, each with different advantages. Using count() Method The count() method is the most straightforward approach. It takes the element as a parameter and returns the number of times it appears in the list ? days = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] element = 'Mon' # Given list and element print("Given list:", days) print("Given element:", element) count = ...

Read More

Count occurrences of a character in string in Python

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

We are given a string and a character, and we want to find out how many times the given character is repeated in the string. Python provides several approaches to count character occurrences. Using count() Method The simplest approach is using Python's built-in count() method ? text = "How do you do" char = 'o' print("Given String:", text) print("Given Character:", char) print("Number of occurrences:", text.count(char)) Given String: How do you do Given Character: o Number of occurrences: 4 Using range() and len() We can design a for loop to ...

Read More
Showing 9031–9040 of 25,466 articles
« Prev 1 902 903 904 905 906 2547 Next »
Advertisements