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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:56:27+00:00 2026-05-29T15:56:27+00:00

I am new to python and am learning how to do things the right

  • 0

I am new to python and am learning how to do things the right way.

I have list of dictionaries d. Each dictionary represents users, and contains information like user_id, age, etc. This list d can contain several dictionaries that represent the same user (but with slightly different information that does not matter for my purposes). I want to create histogram that shows how many users are in d with given age. How to do it in efficient way?

Edit:
I want to emphasise that I need to eliminate duplicates in the list.

  • 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-29T15:56:28+00:00Added an answer on May 29, 2026 at 3:56 pm

    Well, the classic approach to this problem would be to create a defaultdict:

    import collections
    histogram = collections.defaultdict(int)
    

    Then iterate over the dictionaries in the list, and (using d_list instead of d as the name of the list of dictionaries),

    for d in d_list:
        histogram[d['age']] += 1
    

    But you included additional information that confuses me. You said multiple dicts could represent the same user. Do you want to eliminate those duplicates from the histogram? If that’s your question, one approach would be to store the users in a dict of user_records using (firstname, lastname) tuples as keys. Then successive dictionaries representing the same user would smash one another and only one record per user would be preserved. Then iterate over the values in that dictionary (perhaps using user_records.itervalues()).

    This general approach can be modified to use whatever values in each record best identifies unique users. If the user_id value is unique per user, then use that as the key instead of (firstname, lastname). But your question suggested (to me) that the user_id wouldn’t necessarily be the same for two users who are the same.

    Once you have the eliminated duplicates, though, there’s also a shortcut if you’re using Python >= 2.7:

    histogram = collections.Counter(d['age'] for d in user_records.itervalues())
    

    Some example code… say we have a record_list:

    >>> record_list
    [{'lastname': 'Mann', 'age': 23, 'firstname': 'Joe'}, 
     {'lastname': 'Moore', 'age': 23, 'firstname': 'Alex'}, 
     {'lastname': 'Sault', 'age': 33, 'firstname': 'Marie'}, 
     {'lastname': 'Mann', 'age': 23, 'firstname': 'Joe'}]
    >>> user_ages = dict(((d['firstname'], d['lastname']), d['age']) for d in record_list)
    >>> user_ages
    {('Joe', 'Mann'): 23, ('Alex', 'Moore'): 23, ('Marie', 'Sault'): 33}
    

    As you can see, the record_list has a duplicate, but the user_ages dict doesn’t. Now getting a count of ages is as simple as running the values through a Counter.

    >>> collections.Counter(user_ages.itervalues())
    Counter({23: 2, 33: 1})
    

    The same thing can be done with any string or immutable object that can serve as a unique identifier of a particular user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new two python and am trying to grow a dictionary of dictionaries. I
I'm new to python, and have a list of longs which I want to
Still new to python and django, though learning ;-) I have a view that
I am learning Python. I have a function readwrite(filename, list) . filename is of
I am new to python and just started learning it today. I have installed
I am new to programming and am learning Python as my first language. I
I am really new to Python and I have been looking for an example
So I'm really new to programming, I just started learning Python yesterday and I'm
I am learning Python and so far I can tell the things below about
I'm brand new to Eclipse (learning Python). I'm really confused about the terminology within

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.