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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:51:21+00:00 2026-05-22T19:51:21+00:00

I have a list of objects representing widgets. Each widget object has a manufactured

  • 0

I have a list of objects representing widgets. Each widget object has a manufactured DateTime field that holds the date and time when the widget was made. All the widgets in the list were manufactured in a particular year.

I would like to get a list with the total widgets manufactured each month – e.g.:

>>> totals
[1, 5, 819, 187, 1, 5, 15, 9, 13, 77, 54, 22]

So in the above list there were 819 widgets made in March.

What’s a pythonic way of doing this?

  • 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-05-22T19:51:22+00:00Added an answer on May 22, 2026 at 7:51 pm
    totals = [0] * 12
    for widget in widgets:
        totals[widget.datetime.month - 1] += 1
    

    The indices of totals are in the range 0 to 11, while months usually are in the range 1 to 12, so we need the - 1.

    An alternative in Python 2.7 or 3.1 or above:

    from collections import Counter
    totals = Counter(widget.datetime.month for widget in widgets)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects, which has a method that has a couple
I have a list of objects I wish to sort based on a field
I have a list of objects, each containing an Id, Code and Description. I
I have a list of Date objects, and a target Date. I want to
I have a class Player that contains a list of Accessory objects. There are
I have an object that has properties of another object and one called DataValue
I have list of objects that contain information about which classes static EventHandler should
I've got a list of objects that have a Name and a list of
I have a core data application which displays a list of custom objects, each
I have a List of Tasks that a user can preform. Each one will

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.