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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:21:39+00:00 2026-06-06T20:21:39+00:00

going thru a tutorial on python lists,I tried to write a python function which

  • 0

going thru a tutorial on python lists,I tried to write a python function which counts the number of occurrences of words that start with a specific letter

def count_occurrences(p,letter):
    count = 0
    for elem in p:
        if elem[0]==letter:
            count = count+1
    return count


>>>count_occurrences(['damon','jim','dennis'],'d')
2
>>>count_occurrences(['damon','jim'],'d')
1
>>>count_occurrences([],'d')
0

but, if I input a list containing the wrong types,say [1,2,3] ,it will throw a TypeError:'int' object is unsubscriptable since the code elem[0] is called on an int.

So,how do I handle this? should I use a try : except block or is there another way?

  • 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-06T20:21:41+00:00Added an answer on June 6, 2026 at 8:21 pm

    add an isinstance() condition to your if comdition to check the type of elem:

    def count_occurrences(p,letter):
        count = 0
        for elem in p:
            if isinstance(elem,str) and elem[0]==letter: #if the first condition is true then
                                                         # only it'll check the next condition
                count = count+1
        return count
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Going through happstack-lite tutorial : we build functions that have return type of ServerPart
I was going through some java code(which uses playframework ) that provides some shopping
I am going thru a sample code on Apple developer resource, tabster and I
Going mad now. I have a MVC solution that i've upgraded from MVC 1
Going through the microsoft authentication tutorial listed here they have you create a master
I am learning Python and building my first web app. I have been going
I've made this nice python app that runs when i use python main.py in
I am researching Prism v2 by going thru the quickstarts. And I have created
I need to monitor several systems going thru stress testing. I want to make
I have a function which opens up the help file for the app. The

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.