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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:22:54+00:00 2026-06-04T15:22:54+00:00

I use setdefault to count instances like this (this is the stripdown version): user_to_count_map

  • 0

I use setdefault to count instances like this (this is the stripdown version):

user_to_count_map = {}
for username in list_of_usernames:
    x = user_to_count_map.setdefault(username, 0)
    x += 1
    user_to_count_map[username] = x + 1
for username in sorted(usernmae_to_count_map):
    print username, user_to_count_map[username]

I don’t like the assigning back to the map, because the real code is more complex with multiple count increas. But I do seem to need to do that. Is there an easy way around that?

  • 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-04T15:22:56+00:00Added an answer on June 4, 2026 at 3:22 pm

    You could make the counter a list with one element, effectively making it a mutable:

    user_to_count_map = {}
    for username in list_of_usernames:
        x = user_to_count_map.setdefault(username, [0])
        x[0] += 1
    for username, counter in sorted(user_to_count_map.items()):
        print username, counter[0]
    

    I am not sure if that makes your code any more readable, as explicit is better than implicit.

    Or if use python 2.7 or newer (or use a convenient backport), you could use a Counter object:

    from collections import Counter
    user_to_count_map = Counter()
    for username in list_of_usernames:
        user_to_count_map[username] += 1
    for username, counter in sorted(user_to_count_map.items()):
        print username, counter[0]        
    

    Note that by using a Counter you have a dictionary that automatically gives you a default value of 0. It otherwise acts like a dictionary holding integer values, so you can increment and decrement these values any way you like (including adding more than 1).

    The same effect can be had with defaultdict, also in the collections module, but note that the Counter class offers functionality. defaultdict is present in python 2.5 and up; example:

    from collections import defaultdict
    user_to_count_map = defaultdict(lambda: 0)
    for username in list_of_usernames:
        user_to_count_map[username] += 1
    

    Alternatively, you could just dispense with the setdefault altogether, as you are always assigning back to the mapping anyway:

    user_to_count_map = {}
    for username in list_of_usernames:
        x = user_to_count_map.get(username, 0)
        x += 1
        user_to_count_map[x] = x
    for username, counter in sorted(user_to_count_map.items()):
        print username, counter[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a decorator that looks like this def login_required_message(*args, **kwargs): kwargs.setdefault('message', You must
use this website a lot but first time posting. My program creates a number
I know you can use setdefault(key, value) to set default value for a given
How do I use setdefault in python for nested dictionary structures. eg.. self.table[field] =
becouse textboxes are stored in viewstate i cannot use this JS code <script type=text/javascript>
I use the code below to set default values for input objects. This works
Here is what I use: - MacOS X 10.6.7 - GNU bash, version 3.2.48(1)-release
in Google Analytics, if I use _trackEvent, will it increase the page view count?
I use the code bellow Locale locale = new Locale(ar); Locale.setDefault(locale); Configuration config =
I use this date picker . I need input to be empty on page

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.