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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:42:54+00:00 2026-06-02T16:42:54+00:00

Using python 2.6: I have a dictionary where each key contains a list of

  • 0

Using python 2.6:

I have a dictionary where each key contains a list of values.

I want to look at all the values in the dictionary and tally how many times each appears across all keys.

I have been looking at itervalues() or

for value in dictionary.values():

for a start, and also the .count() function, but I need to return a histogram.

for example:

print dictionary

would return

{'test' : ['spam', 'eggs', 'cheese', 'spam'], 'test2' : ['spam', 'cheese', 'goats']}

and I want something to tell me:

{'spam' : 3, 'eggs' : 1, 'cheese': 2, 'goats' : 1}
  • 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-02T16:42:57+00:00Added an answer on June 2, 2026 at 4:42 pm

    Iterate through the list of values and add them to a new dictionary by incrementing one by one.

    # start with an empty output dictionary
    out = {}
    
    # iterate through the keys in the dictionary
    for key in p:
       # iterate through the values in the sublist
       for val in p[key]:
          # check to see if we've seen this one before
          if not out.has_key(val):
             # if not, start it out at 0
             out[val] = 0
    
          # increment by one because we've seen it once more
          out[val] += 1
    
    print out
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve
I have a process that pickles a dictionary using Python 3.2. I then need
I have a python logger set up, using python's logging module. I want to
I have a dictionary where entry values can reference another entry by key eventually
I have problem using dict in python, I just end up with empty dictionary.
Does Python have a built-in, simple way of encoding/decoding strings using a password? Something
I have a working application using python and zeromq and I would like to
I'm using Python, and I have a file which has city names and information
I have been asked to write a program using python for an assignment. I
I have the following script for sending mails using python import smtplib from email.mime.multipart

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.