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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:26:49+00:00 2026-05-28T15:26:49+00:00

Hello I am trying to write these python lines in a single line but

  • 0

Hello I am trying to write these python lines in a single line but getting some errors due to the dictionary modifications the code is doing.

for i in range(len(string)):
    if string[i] in dict:
        dict[string[i]] += 1

the general syntax I believe is

abc = [i for i in len(x) if x[i] in array]

Would it be possible for someone to tell me how this might work considering that I am adding 1 to the value in a dictionary

Thanks

  • 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-05-28T15:26:50+00:00Added an answer on May 28, 2026 at 3:26 pm

    What you’re trying to do can be done with dict, a generator expression and str.count():

    abc = dict((c, string.count(c)) for c in string)
    

    Alternative using set(string) (from a comment down below by soulcheck):

    abc = dict((c, string.count(c)) for c in set(string))
    

    Timing

    Seen the comments down below I performed a little testing among this and other answers. (with python-3.2)

    Test functions:

    @time_me
    def test_dict(string, iterations):
        """dict((c, string.count(c)) for c in string)"""
        for i in range(iterations):
            dict((c, string.count(c)) for c in string)
    
    @time_me
    def test_set(string, iterations):
        """dict((c, string.count(c)) for c in set(string))"""
        for i in range(iterations):
            dict((c, string.count(c)) for c in set(string))
    
    @time_me
    def test_counter(string, iterations):
        """Counter(string)"""
        for i in range(iterations):
            Counter(string)
    
    @time_me
    def test_for(string, iterations, d):
        """for loop from cha0site"""
        for i in range(iterations):
            for c in string:
                if c in d:
                    d[c] += 1
    
    @time_me
    def test_default_dict(string, iterations):
        """defaultdict from joaquin"""
        for i in range(iterations):
            mydict = defaultdict(int)
            for mychar in string:
                mydict[mychar] += 1
    

    Test execution:

    d_ini = dict((c, 0) for c in string.ascii_letters)
    words = ['hand', 'marvelous', 'supercalifragilisticexpialidocious']
    
    for word in words:
        print('-- {} --'.format(word))
        test_dict(word, 100000)
        test_set(word, 100000)
        test_counter(word, 100000)
        test_for(word, 100000, d_ini)
        test_default_dict(word, 100000)
        print()
    
    print('-- {} --'.format('Pride and Prejudcie - Chapter 3 '))
    
    test_dict(ch, 1000)
    test_set(ch, 1000)
    test_counter(ch, 1000)
    test_for(ch, 1000, d_ini)
    test_default_dict(ch, 1000)
    

    Test results:

    -- hand --
    389.091 ms -  dict((c, string.count(c)) for c in string)
    438.000 ms -  dict((c, string.count(c)) for c in set(string))
    867.069 ms -  Counter(string)
    100.204 ms -  for loop from cha0site
    241.070 ms -  defaultdict from joaquin
    
    -- marvelous --
    654.826 ms -  dict((c, string.count(c)) for c in string)
    729.153 ms -  dict((c, string.count(c)) for c in set(string))
    1253.767 ms -  Counter(string)
    201.406 ms -  for loop from cha0site
    460.014 ms -  defaultdict from joaquin
    
    -- supercalifragilisticexpialidocious --
    1900.594 ms -  dict((c, string.count(c)) for c in string)
    1104.942 ms -  dict((c, string.count(c)) for c in set(string))
    2513.745 ms -  Counter(string)
    703.506 ms -  for loop from cha0site
    935.503 ms -  defaultdict from joaquin
    
    # !!!: Do not compare this last result with the others because is timed
    #      with 1000 iterations instead of 100000
    -- Pride and Prejudcie - Chapter 3  --
    155315.108 ms -  dict((c, string.count(c)) for c in string)
    982.582 ms -  dict((c, string.count(c)) for c in set(string))
    4371.579 ms -  Counter(string)
    1609.623 ms -  for loop from cha0site
    1300.643 ms -  defaultdict from joaquin
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this
Hello everyone I'm trying to improve my Java skills by solving some problems from
Hello there i'm trying to write a layout similar to the timepicker widget layout.
Newbie here with a frustrating, but probably simple question. I am trying to write
Hello I am analyzing some data and trying to use a package that contains
I'm trying to write some PowerShell functions that do some stuff and then transparently
I'm trying to write a simple python web application using the Tornado web server
Hello I'm trying to write a webserver in C#. The server is going to
I'm new to Python and trying to write a program with tkinter. Why is
I'm trying to write a simple java program in Eclipse that prints these four

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.