Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 812 of 855
Find most frequent element in a list in Python
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 MoreFind maximum value in each sublist in Python
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 MoreCount tuples occurrence in list of tuples in Python
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 MoreCount the sublists containing given element in a list in Python
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 MoreCount the Number of matching characters in a pair of string in Python
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 MoreCount of elements matching particular condition in Python
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 MoreCount occurrences of an element in a list in Python
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 MoreCount occurrences of a character in string in Python
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 MoreCount number of items in a dictionary value that is a list in Python
Sometimes we have a dictionary where some values are lists and we need to count the total number of items across all these list values. Python provides several approaches to achieve this using isinstance() to check if a value is a list. Using isinstance() with Dictionary Keys We can iterate through dictionary keys and use isinstance() to identify list values ? # defining the dictionary data_dict = {'Days': ["Mon", "Tue", "Wed", "Thu"], 'time': "2 pm", 'Subjects':["Phy", "Chem", "Maths", "Bio"] } print("Given dictionary:", data_dict) ...
Read MoreConverting list string to dictionary in Python
Sometimes you need to convert a string that looks like a list with key-value pairs into an actual Python dictionary. For example, converting '[Mon:3, Tue:5, Fri:11]' into {'Mon': '3', 'Tue': '5', 'Fri': '11'}. Python provides several approaches to handle this conversion. Using split() and Dictionary Comprehension This approach uses split() to separate elements and slicing to remove the brackets, then creates a dictionary using comprehension ? string_data = '[Mon:3, Tue:5, Fri:11]' # Given string print("Given string:", string_data) print("Type:", type(string_data)) # Using split and slicing result = {sub.split(":")[0]: sub.split(":")[1] for sub in string_data[1:-1].split(", ")} ...
Read More