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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:58:25+00:00 2026-06-09T00:58:25+00:00

I am a beginner struggling with Python every day. I have a large data

  • 0

I am a beginner struggling with Python every day. I have a large data set that has Name of animals in the 2nd column. I have a program to add up the count of each animal by its name (each row has 1 animal Name and 1 “count” data). I am trying to get the sum of those count data I obtained using Python but I am not being able to do that. The code I have so far is:

import csv, collections

reader=csv.reader(open('C:\Users\Owl\Data.txt','rb'), delimiter='\t')

counts=collections.Counter()

for line in reader:
   Name=line[1]
   counts[Name]+=1

for (Name, count) in sorted(counts.iteritems()):
   Output=list('%s' % count) #Make output string to a list
   Sum=sum(Output) # Sum function requires a list
   print 'Total kinds of Animals: %s' % Sum

I get an error saying " File "sum_count.py", line 17, in <module> Sum=sum(Output) # Sum function requires a list TypeError: unsupported operand type(s) for +: 'int' and 'str'".

What I have figured out so far is that because sum apparently require the input type to be a list, I converted the count data (which was string) to a list but when I do Output=list('%s' % count), it seems that all the count data that are more than 2 digits are split. For example, when I print Output, it will be like this:

['1', '6', '3']
['3']
['1', '8', '5', '9']
['7', '9']

instead of

['163']
['3']
['1859']
['79']

What I want to do here is to get a single “sum” of these elements. Here, it will be 4. Four kinds of animals.

I am thinking that this may be the reason why I am getting the error above. I might be wrong but could somebody please help me how to solve this issue? Thank you for your help in advance!

  • 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-09T00:58:27+00:00Added an answer on June 9, 2026 at 12:58 am

    I don’t think you need to use sum.

    Try this:

    for (Name, count) in sorted(counts.iteritems()):
        print 'Species total: %s' % count
    

    Or, possibly better:

    for (Name, count) in sorted(counts.iteritems()):
        print 'Total for species %s: %s' % (Name, count)
    

    sum is for when you have a list of numbers and want to find the sum of that list of numbers.
    You have already collected the sum number of each animal using counts — you just need to display it.

    Edit

    To sum up the total number of animals counted, you can do this:

    total = sum(counts.values())
    print 'Total number of animals: %d' % total
    

    Edit 2

    The number of kinds of animals counted is simply the length of the counts dictionary:

    print 'Number of kinds of animals: %d' % len(counts)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

beginner to python here. I have 2 nested lists that I want to merge:
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Python beginner here, I have a list of lists and want to refer to
Beginner programmer here....hope it makes sense :) I have created a console app that
Beginner programmer here. So bear with me. I have a simple python program. print
I am a beginner with PYTHON and useless with regex and are struggling to
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
I'm a complete beginner to SQL and I am struggling to find a solution
Beginner iOS dev here. I have a problem in my array. Im making an
Beginner in Android development. My code crashes. I have made a simple Java method

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.