

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add the occurrence of each number as sublists in Python
We have a list whose elements are numeric. Many elements are present multiple times. We want to create sub list so the frequency of each element along with the elements itself.
With for and append
In this approach we will compare each element in the list with every other elements after it. If there is a match then count will be incremented and both the element and the count will be made into a subsist. List will be made which should contain subsists showing every element and its frequency.
Example
def occurrences(list_in): for i in range(0, len(listA)): a = 0 row = [] if i not in listB: for j in range(0, len(listA)): # matching items from both positions if listA[i] == listA[j]: a = a + 1 row.append(listA[i]) row.append(a) listB.append(row) # Eliminate repetitive list items for j in listB: if j not in list_uniq: list_uniq.append(j) return list_uniq # Caller code listA = [13,65,78,13,12,13,65] listB = [] list_uniq = [] print("Number of occurrences of each element in the list: ") print(occurrences(listA))
Output
Running the above code gives us the following result −
Number of occurrences of each element in the list: [[13, 3], [65, 2], [78, 1], [12, 1]]
With Counter
We use counter method from the collections module. It will give the count of every element in the list. Then we declare a new empty list and add the key value pair for each item in form of element and its count into the new list.
Example
from collections import Counter def occurrences(list_in): c = Counter(listA) new_list = [] for k, v in c.items(): new_list.append([k, v]) return new_list listA = [13,65,78,13,12,13,65] print("Number of occurrences of each element in the list: ") print(occurrences(listA))
Output
Running the above code gives us the following result −
Number of occurrences of each element in the list: [[13, 3], [65, 2], [78, 1], [12, 1]]
- Related Questions & Answers
- First occurrence of True number in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Python program to find occurrence to each character in given string
- Python program to remove each y occurrence before x in List
- Java program to check occurrence of each character in String
- Java program to check occurrence of each vowel in String
- Python Pandas - Count the number of rows in each group
- Program to find number of sublists whose sum is given target in python
- Program to count number of sublists with exactly k unique elements in Python
- Python Pandas - Return the midpoint of each Interval in the IntervalArray as an Index
- Add 1 to the number represented as array (Recursive Approach)?
- Java program to count the occurrence of each character in a string using Hashmap
- Program to find number of sublists that contains exactly k different words in Python
- python Pandas - Return the left endpoints of each Interval in the IntervalArray as an Index
- Python Pandas - Return the right endpoints of each Interval in the IntervalArray as an Index