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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:19:46+00:00 2026-05-16T07:19:46+00:00

Given a python class class Student(): and a list names = [] ; then

  • 0

Given a python class class Student(): and a list names = []; then I want to create several instances of Student() and add them into the list names,

names = [] # For storing the student instances
class Student():
    def __init__(self, score, gender):
        self.score = score
        self.gender = gender

And now I want to check out the scores of all the male students, can I do it like this?

scores = []
for i in names:
    if i.gender ==  "Male":
        scores.append(i.score)

My question is: How to create a list that can (if could be done by any statement) store the instance of Student? Or rather, when I write names = [], how could I state every element in names is an instance of Student so that I can use the attributs of this element despite python is weak type? I hope I made myself clear 😉

Can I write like:

    for i in range(len(names)):
        student = Student()
        student = names[i]
        if student.gender == "Male":
            # Whatever

I guess not…

  • 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-16T07:19:46+00:00Added an answer on May 16, 2026 at 7:19 am

    Did you try your code above? It should work fine. You can condense it into:

    scores = [ student.name for student in names if student.gender == "Male" ]
    

    Note that calling the list names is misleading, since it is a list of Student instances.

    You can’t define the list to be a list of Student instances; that’s not how Python works.

    Are you asking how to create the list that you’ve called names?

    names = [ ]
    for ( score, gender ) in <some-data-source>:
        names.append( Student( score, gender ) )
    

    which is of course equivalent to

    names = [ Student( score, gender ) for score, gender in <some-data-source> ]
    

    and in turn to

    names = [ Student( *row ) for row in <some-data-source> ]
    

    If you need to do a lot of processing for each row then you can either move the processing into a separate function or use a for loop.

    def process_row( row ):
        ...
        return score, gender
    
    names = [ Student( *process_row( row ) ) for row in <some-data-source> ]
    

    Responding to your edit, I think you are trying to declare the types of variables in Python. You wrote:

    for i in range(len(names)):
        student = Student()
        student = names[i]
        if student.gender == "Male":
            # Whatever
    

    What is the purpose of the line student = Student() — are you trying to declare the type of the variable student? Don’t do that. The following will do what you intended:

    for student in students:
       if student.gender == "Male":
           # Whatever
    

    Notice several things:

    1. We don’t need to iterate over range(n) and then look up each instance in names; iterating over every element of a container is the purpose of a for loop.
    2. You don’t need to make any claims about what student is — it could be a string, a boolean, a list, a Student, whatever. This is dynamic typing. Likewise, students doesn’t have to be a list; you can iterate over any iterable.
    3. When you write student.gender, Python will get the gender attribute of student, or raise an exception if it doesn’t have one.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Google Python Class | List Exercise - Given a list of numbers, return a
Fromg Google's Python Class: E. Given two lists sorted in increasing order, create and
This exercise is taken from Google's Python Class : D. Given a list of
given a Python class hierarchy, say class Base: def method1 def method2 def method3
Given: alphabet = ['a','b','c',...,'z'] i want python to enumerate every combination (starting from 1
Given a custom, new-style python class instance, what is a good way to hash
I'd like to create a Python class decorator (*) that would be able to
I've stumbled in a tricky python question. Given ( updated ): class A(object): def
Given a string of a Python class, e.g. my_package.my_module.MyClass , what is the best
I've found a simply way to implement(hack) an enum into Python: class MyEnum: VAL1,

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.