Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Add the occurrence of each number as sublists

  • Last Updated : 23 Apr, 2020

Given a list of numbers, the task is to count the occurrence of each element and add as sublists.

Examples:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input: l1 = [3, 5, 7, 2, 3, 5, 9.1]
Output: [[3, 2], [5, 2], [7, 1], [2, 1], [9.1, 1]]

Input: l1 = [1, 1, 2, 2, 3, 1]
Output: [[1, 3], [2, 2], [3, 1]]

Note: Format output 2d list is [[‘item1’, ‘count1’], [‘item2’, ‘count2’], …, [‘itemN’, ‘countN’]].



Code #1: Using count() method




# Python program to add the occurrence
# of each number as sublists
def count_occur(list1, **kwargs):
      
    # iterate over list item
    for i in list1:
        row =[]
        ct = 0
      
        # count function will count occurrence
        ct = list1.count(i)
        row.append(i)
        row.append(ct)
        # append 1d list items to 2d list
        list2.append(row)
      
    # below code is to eliminate 
    # repetitive list items
    for j in list2:
        if j not in unq_l2:
            unq_l2.append(j)
              
    return unq_l2
  
# Driver Code
l1 = [3, 5, 7, 2, 3, 5, 9.1]
list2 = []
unq_l2 = []
print(count_occur(l1))
Output:
[[3, 2], [5, 2], [7, 1], [2, 1], [9.1, 1]]

 
Code #2: Using loop method




def count_occur(list1):
  
    for i in range(0, len(l1)):
        a = 0
        row =[]
        if i not in l:
            for j in range(0, len(l1)):
  
                # matching items from both lists
                if l1[i]== l1[j]:
  
                    # on match counter increments by 1
                    a = a + 1
  
            row.append(l1[i])
            row.append(a)
  
            # append function will append 
            # 1d list items to 2d list
            l.append(row)
              
    # below code is to eliminate
    # repetitive list items    
    for j in l:
        if j not in unq_l:
            unq_l.append(j)
              
    return unq_l
  
# Driver code        
l1 =[3, 5, 7, 2, 3, 5, 9.1]
l =[]
unq_l =[]
  
print(count_occur(l1)) 
Output:
[[3, 2], [5, 2], [7, 1], [2, 1], [9.1, 1]]

Code #3: Using counter() method




# Python program to add the occurrence
# of each number as sublists using counter() method
  
from collections import Counter
def count_occurence(l):
    c = Counter(l)
    l1 = []
    for k,v in c.items():
        l1.append([k,v])
    return l1
  
  
# Driver code     
l = [3, 5, 7, 2, 3, 5, 9.1]
print(count_occurence(l))
Output:
[[2, 1], [3, 2], [9.1, 1], [5, 2], [7, 1]]



My Personal Notes arrow_drop_up
Recommended Articles
Page :