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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:19:04+00:00 2026-06-09T04:19:04+00:00

I am using the Counter class in Python to count the number of words

  • 0

I am using the Counter class in Python to count the number of words in a key of a large python dictionary. I am creating a new dictionary with the keys being incremented numbers and the values being the return of the counter function. This is what my new dictionary looks like:

TP = {1:Counter({u'x':1, u'b':1, u'H':3}),2:Counter({u'j':1, u'm':4, u'e':2})...}

What I want to do, if possible, is remove the Counter and the parentheses ( and ) so that my dictionary looks like:

TP = {1:{u'x':1, u'b':1, u'H':3}, 2:{u'j':1, u'm':4, u'e':2}...}

If that’s not possible, can someone please tell me how to access the nested dictionaries inside the ()? Thank you

  • 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-09T04:19:05+00:00Added an answer on June 9, 2026 at 4:19 am

    I don’t see any point of doing this, Counter is a subclass of dict and supports all of it’s methods so you should have no problems leaving it as is, although just for the sake of this question I will convert it back to a normal dict:

    >>> from collections import Counter
    >>> tp = {1:Counter({u'x':1, u'b':1, u'H':3}),2:Counter({u'j':1, u'm':4, u'e':2})}
    >>> dict((k, dict(v)) for k, v in tp.iteritems())
    {1: {u'x': 1, u'b': 1, u'H': 3}, 2: {u'e': 2, u'j': 1, u'm': 4}}
    

    For Python 2.7+ (as suggested by @Blender)

    {k: dict(v) for k, v in tp.iteritems()}
    

    Iterating through subdictionaries eg:

    for w, c in tp[1].iteritems():
        print w, c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to monitor the performance information for openoffice using the performance counter class
I am using python to increment a counter in 5 minute intervals in redis
I am getting information from a large xml file and I am using python
I have created a custom performance counter using the following code: public class PerfCounter
I am using an FPS counter class from an open source project and I
The following code using System.Threading; class Test { volatile int counter = 0; public
I’m trying to make an RSS subscribers counter using PHP and don’t know where
The perfmon counter is using different NIC names compare to ipconfig/all and c# system
I'm using mongodb 2.1 and how to translate this query into php db.counter.aggregate([ {
I'm experimenting with using html5 and css counters to number the figures in a

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.