Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: pub@towardsai.net
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab VeloxTrend Ultrarix Capital Partners Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Our 15 AI experts built the most comprehensive, practical, 90+ lesson courses to master AI Engineering - we have pathways for any experience at Towards AI Academy. Cohorts still open - use COHORT10 for 10% off.

Publication

Count python list item occurrences
Latest

Count python list item occurrences

Last Updated on July 17, 2021 by Editorial Team

Author(s): Vivek Chaudhary

Programming

From a list of integers, count the occurrence of each element and add items and count as a sub-list.

Count python list item occurrences

Input list: inp = [7, 9, 7, 4, 3, 5, 3, 6, 9, 3]
Output list: out= [[7, 2], [9, 2], [4, 1], [3, 3], [5, 1], [6, 1]]

#Note: where 7 is list item and 2 is the count of occurrence

Using nested loops

def count_items(inp):
    for i in range(0, len(inp)):
a = 0
row =[]
if i not in l:
for j in range(0, len(inp)):
if inp[i]== inp[j]:
a = a + 1
row.append(inp[i])
row.append(a)
l.append(row)
for j in l:
if j not in out:
out.append(j)
return out

Call the function and check the output:

#call func() 
inp = [7, 9, 7, 4, 3, 5, 3, 6, 9, 3]
l = []
out = []
print(count_items(inp))
Output: [[7, 2], [9, 2], [4, 1], [3, 3], [5, 1], [6, 1]]

Using count() method

def count_items(inp):
    for i in inp:
row =[]
ct = 0
ct = inp.count(i)
row.append(i)
row.append(ct)
l.append(row)
   for j in l:
if j not in out:
out.append(j)
return out

Call the function and check the output:

#call func()
inp = [7, 9, 7, 4, 3, 5, 3, 6, 9, 3]
l = []
out = []
print(count_items(inp))
Output: [[7, 2], [9, 2], [4, 1], [3, 3], [5, 1], [6, 1]]

Using counter() method

from collections import Counter
def count_items(inp):
coll = Counter(inp)
out = []
for key,val in coll.items():
out.append([key,val])
return out

Call the function and check the output:

#call func() 
inp = [7, 9, 7, 4, 3, 5, 3, 6, 9, 3]
print(count_items(inp))
Output: [[7, 2], [9, 2], [4, 1], [3, 3], [5, 1], [6, 1]]

To summarize, we covered several ways to count occurrences of list items as below:

  • count() method
  • nested for loops
  • collections library counter() method

That’s all with count occurrences of list items.

Thanks for reading my blog and supporting the content. Appreciation always helps to keep up the spirit. Will try my best to keep coming up with high-quality content. Connect with me to get updates about upcoming new content.


Count python list item occurrences was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI


Take our 90+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Towards AI has published Building LLMs for Production—our 470+ page guide to mastering LLMs with practical projects and expert insights!


Discover Your Dream AI Career at Towards AI Jobs

Towards AI has built a jobs board tailored specifically to Machine Learning and Data Science Jobs and Skills. Our software searches for live AI jobs each hour, labels and categorises them and makes them easily searchable. Explore over 40,000 live jobs today with Towards AI Jobs!

Note: Content contains the views of the contributing authors and not Towards AI.