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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:53:07+00:00 2026-06-09T02:53:07+00:00

So I have a dictionary d = {‘a’: [4,’Adam’, 2, ‘Bob’, 3], ‘b’: [3,’John’,

  • 0

So I have a dictionary

d = {'a': [4,'Adam', 2, ‘Bob’, 3], 'b': [3,'John', 4, ‘Bill’], 'c': [4,'Adam', 3, 4, ‘John’], 'd': [4,'Bill', ‘Joe’, 3], 'e': [4,'Bob', ‘Bob’, 5, 8, 10], 'f': [4, 'Joe'], 'g': [4, 'Bill', 4, ‘Joe’, 1]}

From which I want to return the counts of the names, such as

Adam: 2
Bill: 3
John: 2
Bob: 3
Joe: 3

I have tried using the collections counter through this function:

x = 0
for vals in d.itervalues(): 
    while x<len(vals):           
        if type(vals[x]) == str:
            print Counter([vals[x]])
        x = x+1 

Which returns

Counter({'Adam': 1})
Counter({'Bob': 1})
Counter({'Adam': 1})
Counter({'John': 1})
Counter({'John': 1})
Counter({'Bill': 1})
Counter({'Bob': 1})
Counter({'Bob': 1})
Counter({'Bill': 1})
Counter({'Joe': 1})
Counter({'Bill': 1})
Counter({'Joe': 1})
Counter({'Joe': 1})

But that isn’t the result I want. Would I just need to add the counters? How would I do that? I know the counter class has an add/ subtract function but the methods I’m trying aren’t working.

I’ve also tried something like this to print out different counters for each variation.

'c%s'%(x)

Which would set c1, c2, c3 for each counter and then I could just add them with c1 + c2 + c3 to get the result I want. However, I am unable to print these different counter lists out.

Along with that, once it does return a result, it always returns “Counter({__})”. How would I go about deleting that portion or just printing out the names and count solely?

Thanks for the help!

  • 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-09T02:53:10+00:00Added an answer on June 9, 2026 at 2:53 am

    You are creating a new Counter instance for each item you are counting, where you need a single instance to count them all:

    from collections import Counter
    counter = Counter(s for v in d.itervalues() for s in v if isinstance(s, str))
    

    This results in

    Counter({'Bob': 3, 'Bill': 3, 'Joe': 3, 'John': 2, 'Adam': 2})
    

    Note that you should generally avoid the kind of heterogeneous lists that occurs in your dictionary values if possible.

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

Sidebar

Related Questions

I have: dictionary = {foo:12, bar:2, jim:4, bob: 17} I want to iterate over
I have C# Dictionary and I want create a .csv file from it. For
I have Dictionary from string key i want to get Value of corresponding key
I have a dictionary/glossary which translates from one language to another. When I fill
I have a dictionary of lists for which I want to add a value
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
I want to have Dictionary that would be 'Observable' in order to throw events
I have a Dictionary Byte, Char ASCIIlist that I want to populate with the
I have a Dictionary<string, mystruct> instance, for which I am grabbing a list of
I have dictionary Dictionary<string, Point> the Key is c1,c3,c2,t1,,t4,t2 I want to sort them

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.