Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:08:52+00:00 2026-06-11T12:08:52+00:00

I have a Python list and I want to know what’s the quickest way

  • 0

I have a Python list and I want to know what’s the quickest way to count the number of occurrences of the item, '1' in this list. In my actual case, the item can occur tens of thousands of times which is why I want a fast way.

['1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '7', '7', '7', '10', '10']

Which approach: .count or collections.Counter is likely more optimized?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:08:54+00:00Added an answer on June 11, 2026 at 12:08 pm
    a = ['1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '7', '7', '7', '10', '10']
    print a.count("1")
    

    It’s probably optimized heavily at the C level.

    Edit: I randomly generated a large list.

    In [8]: len(a)
    Out[8]: 6339347
    
    In [9]: %timeit a.count("1")
    10 loops, best of 3: 86.4 ms per loop
    

    Edit edit: This could be done with collections.Counter

    a = Counter(your_list)
    print a['1']
    

    Using the same list in my last timing example

    In [17]: %timeit Counter(a)['1']
    1 loops, best of 3: 1.52 s per loop
    

    My timing is simplistic and conditional on many different factors, but it gives you a good clue as to performance.

    Here is some profiling

    In [24]: profile.run("a.count('1')")
             3 function calls in 0.091 seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    0.091    0.091 <string>:1(<module>)
            1    0.091    0.091    0.091    0.091 {method 'count' of 'list' objects}
    
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Prof
    iler' objects}
    
    
    
    In [25]: profile.run("b = Counter(a); b['1']")
             6339356 function calls in 2.143 seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    2.143    2.143 <string>:1(<module>)
            2    0.000    0.000    0.000    0.000 _weakrefset.py:68(__contains__)
            1    0.000    0.000    0.000    0.000 abc.py:128(__instancecheck__)
            1    0.000    0.000    2.143    2.143 collections.py:407(__init__)
            1    1.788    1.788    2.143    2.143 collections.py:470(update)
            1    0.000    0.000    0.000    0.000 {getattr}
            1    0.000    0.000    0.000    0.000 {isinstance}
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Prof
    iler' objects}
      6339347    0.356    0.000    0.356    0.000 {method 'get' of 'dict' objects}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Python nested list that I'm trying to organize and eventually count number
I have a python list which contains dictionaries and I want to make a
I have a python list as [ (A,{'a':1,'b':2,'c':3,'d':4}), B,{'a':1,'b':2,'c':3,'d':4}), ... ] I want to
I have a list in Python, and I want to check if any elements
If you have a list in python, and want to extract elements at indices
If you have a list in python, and you want the elements from 2
I want to limit the size of a list in python 2.7 I have
Python beginner here, I have a list of lists and want to refer to
I have two lists in python and I want to know if they intersect
I have a list in Python. I don't know exactly how long it is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.