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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:44:27+00:00 2026-06-09T14:44:27+00:00

Following is my coding for count letters and i need the output as [(‘e’,

  • 0

Following is my coding for count letters and i need the output as

[('e', 1), ('g', 2), ('l', 1), ('o', 2)]

and my out put is

[('e', 1), ('g', 2), ('g', 2), ('l', 1), ('o', 2), ('o', 2)]

This is my code

def countLetters(word):
    word=list(word)
    word.sort()
    trans=[]
    for j in word:
        row=[]
        a=word.count(j)
        row.append(j)
        row.append(a)
        trans.append(tuple(row))
    return trans

can anyone explain me, how to get the expected output with my code?
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-09T14:44:29+00:00Added an answer on June 9, 2026 at 2:44 pm

    Why not just use a Counter?

    Example:

    from collections import Counter
    
    c = Counter("Foobar")
    print sorted(c.items())
    

    Output:

    [(‘F’, 1), (‘a’, 1), (‘b’, 1), (‘o’, 2), (‘r’, 1)]


    Another way is to use a dict, or better, a defaultdict (when running python 2.6 or lower, since Counter was added in Python 2.7)

    Example:

    from collections import defaultdict
    
    def countLetters(word):
        d = defaultdict(lambda: 0)
        for j in word:
            d[j] += 1
        return sorted(d.items())
    
    print countLetters("Foobar")
    

    Output:

    [(‘F’, 1), (‘a’, 1), (‘b’, 1), (‘o’, 2), (‘r’, 1)]


    Or use a simple list comprehension

    word = "Foobar"
    print sorted((letter, word.count(letter)) for letter in set(word))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen lot projects following this kind of coding style Any significant advantage
I am working on coding a GUI with C#. I'm following this simple tutorial
Consider the following code (for simplicity, I did not follow any C# coding rules).
I have the following python modules. Sorry if the code is ugly. This is
I'm getting the following error: this class is not key value coding-compliant for the
I have the following jquery code: while (count < 31) { window['cday_' + count]
I've recently started coding in C++ and i'm having doubts about the following code.
Why The constructor ArrayAdapter(new View.OnKeyListener(){}, int, String[]) is undefined in following my coding. This
I use the following coding which edits my text file based on the value
i have following coding to resize image and than save it my virtual folder

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.