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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:34:27+00:00 2026-06-15T13:34:27+00:00

Hey guys so this is my first year programming and I started with python.

  • 0

Hey guys so this is my first year programming and I started with python. I am understanding the programming fairly well but I need help with this homework question.

I have to use a list as my parameter and then return the number of different values in the list. The example list in the question is [1, 4, 1, 7, 6, 1, 4, 3] and therefore the returned value should be 5.

Now I know my method of solving it is probably not concise or elegant but if someone could help me and tell me what to change so it works I would greatly appreciate it.

def count(mylist):
    newlist = []
    newlist.append(mylist[0])
    stor = False
    for i in mylist:
        stor = False
        for j in newlist:
            if j == i:
                stor == True
        if not stor:
            newlist.append(i)
    return newlist
  • 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-15T13:34:28+00:00Added an answer on June 15, 2026 at 1:34 pm

    At first, a fixed version of your program

    def count(mylist):
        newlist = []
        newlist.append(mylist[0])
        stor = False
        for i in mylist:
            stor = False
            for j in newlist:
                if j == i:
                    stor = True # stor == True test for equality
            if not stor:
                newlist.append(i)
        return len(newlist) # you do not want the list itself but its length
    

    And here some suggestions:

    • you do not need to initialize stor outside the outer loop. This is done two lines later again.
    • consider a break in the inner loop – this speeds things up a bit (no unneeded comparison)
    • newlist can be initialized to an empty list without appending the first item. The algorithm stays valid (the inner loop have zero iterations the first time)

    here as code-example:

    def count(mylist):
        newlist = []
        for i in mylist:
            stor = False
            for j in newlist:
                if j == i:
                    stor = True
                    break
            if not stor:
                newlist.append(i)
        return len(newlist)
    

    And more elegant (and pythonic): use the in-syntax 😉

    def count(mylist):
        newlist = []
        for i in mylist:
            if i not in newlist:
                newlist.append(i)
        return len(newlist)
    

    Basically item in something_iterable returns true, if item can be found in something_iterable. Most collection of items is iterable (e.g. lists, sets, strings … 'a' in 'abc' returns true )

    and the most pythonic way but without for/while-loops:

    def count(mylist):
        return len(set(mylist))
    

    Please look into other answers for an explanation.

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

Sidebar

Related Questions

Hey guys, this is my first time actually posting at stackflow but i've used
Hey guys I need some help with this: I have two view controllers, let's
Hey guys this is a bit of a homework puzzle I'm working on and
hey guys having this really simple problem but cant seem to figure out have
Hey guys, this is simple but I can't make it workn... blah! I have
Hey guys I'm not sure how to write this in AS3, but basically I
Hey there guys, this is my first question on Stack Overflow. I figured this
Hey guys I'm trying to work my way through this but am having an
Hey guys, I'm very excited about how experienced I am in programming. The first,
Hey guys, hoping for some help with this :( been stuck on it for

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.