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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:05:42+00:00 2026-06-18T05:05:42+00:00

I have just started learning python and worried that if I use dict.get(key,default_value) or

  • 0

I have just started learning python and worried that if I use dict.get(key,default_value) or I define my own method for it….so do they have any differences:

[1st method]:

dict={}
for c in string:
    if c in dict:
        dict[c]+=1
    else:
        dict[c]=1

and the other dict.get() method that python provides

for c in string:
    dict[c]=dict.get(c,0)+1

do they have any differences on efficiency or speed…or they are just the same and 2nd one only saves writing few more lines of code…

  • 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-18T05:05:43+00:00Added an answer on June 18, 2026 at 5:05 am

    For this specific case, use either a collections.Counter() or a collections.defaultdict() object instead:

    import collections
    
    dct = collections.defaultdict(int)
    
    for c in string:
         dict[c] += 1
    

    or

    dct = collections.Counter(string)
    

    Both are subclasses of the standard dict type. The Counter type adds some more helpful functionality like summing two counters or listing the most common entities that have been counted. The defaultdict class can also be given other default types; use defaultdict(list) for example to collect things into lists per key.

    When you want to compare performance of two different approaches, you want to use the timeit module:

    >>> import timeit
    >>> def intest(dct, values):
    ...     for c in values:
    ...         if c in dct:
    ...             dct[c]+=1
    ...         else:
    ...             dct[c]=1
    ... 
    >>> def get(dct, values):
    ...     for c in values:
    ...         dct[c] = dct.get(c, 0) + 1
    ... 
    >>> values = range(10) * 10
    >>> timeit.timeit('test(dct, values)', 'from __main__ import values, intest as test; dct={}')
    22.210275888442993
    >>> timeit.timeit('test(dct, values)', 'from __main__ import values, get as test; dct={}')
    27.442166090011597
    

    This shows that using in is a little faster.

    There is, however, a third option to consider; catching the KeyError exception:

    >>> def tryexcept(dct, values):
    ...     for c in values:
    ...         try:
    ...             dct[c] += 1
    ...         except KeyError:
    ...             dct[c] = 1
    ... 
    >>> timeit.timeit('test(dct, values)', 'from __main__ import values, tryexcept as test; dct={}')
    18.023509979248047
    

    which happens to be the fastest, because only 1 in 10 cases are for a new key.

    Last but not least, the two alternatives I proposed:

    >>> def default(dct, values):
    ...     for c in values:
    ...         dct[c] += 1
    ... 
    >>> timeit.timeit('test(dct, values)', 'from __main__ import values, default as test; from collections import defaultdict; dct=defaultdict(int)')
    15.277361154556274
    >>> timeit.timeit('Counter(values)', 'from __main__ import values; from collections import Counter')
    38.657804012298584
    

    So the Counter() type is slowest, but defaultdict is very fast indeed. Counter()s do a lot more work though, and the extra functionality can bring ease of development and execution speed benefits elsewhere.

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

Sidebar

Related Questions

I have just started learning python and i was wondering how i would get
I have just started learning python/matplotlib/basemap and could really use some help. How do
I have just started learning python version 3 and trying to create a file
I just started using/learning Python and have some questions. I have a text file
I just started learning python and django and I have a question. I got
I have just started learning XNA. This is my first program that I am
I have just started learning UML and after completing use case I've just started
I have just started learning python and am getting caught up. I come from
I only just started learning Python and found out that I can pass a
I started learning Python just today using Python 2.7 and I have a question

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.