
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Element with largest frequency in list in Python
A lot of statistical data analysis tries to find the values which have maximum frequency in a given list of values. Python provides multiple approaches using which we can find such value form a given list. Below are the approaches.
Using Counter
The Counter function from collections module has a options which can directly find the most common element in a given list. We have the most_common function to which we pass a parameter 1 for only one element with highest frequency and pass 2 if we need two elements which have highest frequency.
Example
from collections import Counter # Given list listA = ['Mon', 'Tue','Mon', 9, 3, 3] print("Given list : ",listA) # Adding another element for each element Newlist1 = Counter(listA).most_common(1) Newlist2 = Counter(listA).most_common(2) # Results print("New list after duplication: ",Newlist1) print("New list after duplication: ",Newlist2)
Output
Running the above code gives us the following result −
Given list : ['Mon', 'Tue', 'Mon', 9, 3, 3] New list after duplication: [('Mon', 2)] New list after duplication: [('Mon', 2), (3, 2)]
Using mode
The mode is a statistical function available in the statistics module of python. It will output the element with highest frequency. If there are multiple such elements then the element which is encountered first with highest frequency will be the output.
Example
from statistics import mode # Given list listA = ['Mon', 'Tue','Mon', 9, 3, 3] listB = [3,3,'Mon', 'Tue','Mon', 9] print("Given listA : ",listA) print("Given listB : ",listB) # Adding another element for each element Newlist1 = mode(listA) Newlist2 = mode(listB) # Results print("New listA after duplication: ",Newlist1) print("New listB after duplication: ",Newlist2)
Output
Running the above code gives us the following result −
Given listA : ['Mon', 'Tue', 'Mon', 9, 3, 3] Given listB : [3, 3, 'Mon', 'Tue', 'Mon', 9] New listA after duplication: Mon New listB after duplication: 3
- Related Articles
- Python Program to Find the Largest Element in a Doubly Linked List
- Python – Find the frequency of numbers greater than each element in a list
- List frequency of elements in Python
- Python – Restrict Elements Frequency in List
- Kth Largest Element in an Array in Python
- Kth Largest Element in a Stream in Python
- Find the Second Largest Element in a Linked List in C++
- Java Program to Return the Largest Element in a List
- Python Program to Select the nth Largest Element from a List in Expected Linear Time
- Python – Fractional Frequency of elements in List
- Finding frequency in list of tuples in Python
- Program to find an element in list whose value is same as its frequency in Python
- Element repetition in list in Python
- Python – Count frequency of sublist in given list
- Python Program to find largest element in an array
