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 9020453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:01:15+00:00 2026-06-16T05:01:15+00:00

For a list like this: for i in range(100): things.append({‘count’:1}) for i in range(100):

  • 0

For a list like this:

for i in range(100):
    things.append({'count':1})

for i in range(100):
    things.append({'count':2})

To count the number of 1 in list:

len([i['count'] for i in things if i['count'] == 1])

What is a better way?

  • 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-16T05:01:16+00:00Added an answer on June 16, 2026 at 5:01 am

    collections.Counter

    >>> from collections import Counter
    >>> c = Counter([thing['count'] for thing in things])
    >>> c[1]               # Number of elements with count==1
    100
    >>> c[2]               # Number of elements with count==2
    100
    >>> c.most_common()    # Most common elements
    [(1, 100), (2, 100)]
    >>> sum(c.values())    # Number of elements
    200
    >>> list(c)            # List of unique counts
    [1, 2]
    >>> dict(c)            # Converted to a dict 
    {1: 100, 2: 100}
    

    Perhaps you could do something like this?

    class DictCounter(object):
        def __init__(self, list_of_ds):
            for k,v in list_of_ds[0].items():
                self.__dict__[k] = collections.Counter([d[k] for d in list_of_ds])
    
    >>> new_things = [{'test': 1, 'count': 1} for i in range(10)]
    >>> for i in new_things[0:5]: i['count']=2
    
    >>> d = DictCounter(new_things)
    >>> d.count
    Counter({1: 5, 2: 5})
    >>> d.test
    Counter({1: 10})
    

    Extended DictCounter to handle missing keys:

    >>> class DictCounter(object):
        def __init__(self, list_of_ds):
            keys = set(itertools.chain(*(i.keys() for i in list_of_ds)))
            for k in keys:
                self.__dict__[k] = collections.Counter([d.get(k) for d in list_of_ds])
    
    >>> a = [{'test': 5, 'count': 4}, {'test': 3, 'other': 5}, {'test':3}, {'test':5}]
    >>> d = DictCounter(a)
    >>> d.test
    Counter({3: 2, 5: 2})
    >>> d.count
    Counter({None: 3, 4: 1})
    >>> d.other
    Counter({None: 3, 5: 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list iteration in python defined like this: for i in range(5):
I want to get a list of num like 10,100,1000,10000... I can do this
Is there a way of creating an age select list like this: <?php $Array
I have a list like this: <ul> <li> <span>USA:</span> Newyork <span>USA:</span> Dallas </li> <li>
I have a simple list like this: <ul id=cyclelist> <li>Item 1</li> <li>Item 2</li> <li>Item
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
Given a list like this: num = [1, 2, 3, 4, 5] There are
I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have a list like this: <select name=select_list_name id=list_id> <option value=>Select Option</option> <option value=value1>Option
I have a sortable list like this one: http://jqueryui.com/demos/sortable Is it possible to get

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.