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

  • Home
  • SEARCH
  • 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 6385191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:52:06+00:00 2026-05-25T02:52:06+00:00

I know about list comprehensions, what about dictionary comprehensions? Expected Output: >>> countChar(‘google’) {‘e’:

  • 0

I know about list comprehensions, what about dictionary comprehensions?

Expected Output:

>>> countChar('google')
    {'e': 1, 'g': 2, 'l': 1, 'o': 2}
    >>> countLetters('apple')
    {'a': 1, 'e': 1, 'l': 1, 'p': 2}
    >>> countLetters('')
    {}

Code (I’m a beginner):

def countChar(word):
    l = []
    #get a list from word
    for c  in word: l.append(c)
    sortedList = sorted(l)
    uniqueSet = set(sortedList)
    return {item:word.count(item) for item in uniqueSet }

What is the problem with this code? Why do I get this SyntaxError?

return { item:word.count(item) for item in uniqueSet }
^
SyntaxError: invalid syntax
  • 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-25T02:52:07+00:00Added an answer on May 25, 2026 at 2:52 am

    edit: As agf pointed out in comments and the other answer, there is a dictionary comprehension for Python 2.7 or newer.

    def countChar(word):
        return dict((item, word.count(item)) for item in set(word))
    
    >>> countChar('google')
    {'e': 1, 'g': 2, 'o': 2, 'l': 1}
    >>> countChar('apple')
    {'a': 1, 'p': 2, 'e': 1, 'l': 1}
    

    There is no need to convert word to a list or sort it before turning it into a set since strings are iterable:

    >>> set('google')
    set(['e', 'o', 'g', 'l'])
    

    There is no dictionary comprehension with for Python 2.6 and below, which could be why you are seeing the syntax error. The alternative is to create a list of key-value tuples using a comprehension or generator and passing that into the dict() built-in.

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

Sidebar

Related Questions

Now I know about the normal CSS list styles (roman, latin, etc) and certainly
I know about code-behind files, but what is the best real-world way of Designers
For my assignment , I want to know about common compiler list? I really
Can anyone list custom iPhone UI Components? I know about the Three20 components which
I found a error that I didn't know about, I am using a List,
Possible Duplicate: What USEFUL bitwise operator code tricks should a developer know about? Hi,
Does anyone know about an opensource list of most common error messages? My motivation
I know about Application.Exit() but what if I'm not in a windows forms application
I know about the HIG (which is quite handy!), but what programming practices do
I know about SortedSet , but in my case I need something that implements

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.