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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:27:24+00:00 2026-05-24T11:27:24+00:00

I have defined a custom object with multiple fields. For example say I have

  • 0

I have defined a custom object with multiple fields.

For example say I have a Student object, which consists of a name, ID, and age. To compare two students and determine whether they are the same student or not, I implemented a __ eq__ method that will return whether the age, name, and ID of the two students match up.

def __eq__(self, other):
   return self.name == other.name and self.ID == other.ID and self.age == other.age

Bear in mind that the student is just an example, so the fact that student ID’s tend to be unique is not considered.

Suppose I have the following enrollment lists with an arbitrary number of Student objects

[S1, S2, S3]
[S2, S3]
[S3, S5, S4]
[S1, S4, S2, S1]

I would want to create some data structure that will contain the following elements

S1, S2, S3, S4, S5

The simplest way to do this would be to initialize some data structure that can hold lots of stuff, grab an item, check whether it exists in the structure, and add it if it doesn’t.

new_list = some_new_list 
for each list of students:
  for each student in the list:
     check if the student is in new_list
     #decide what to do 

If I decided to implement it as a simple list, I could potentially make a lot of comparisons as my list continues to grow, especially if I have a ridiculous amount of students and enrollment lists.

What is an efficient way of implementing this? Both for comparing two objects and then using that comparison method to generate a unique set of objects.

EDIT: so I tried a simple set implementation.

>>>a = Student("sample", 1234, 18)
>>>b = Student("sample", 1234, 18)
>>>students = set()
>>>students.add(a)
>>>b in students
False
>>>b == a
True

Am I doing something wrong?

  • 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-24T11:27:24+00:00Added an answer on May 24, 2026 at 11:27 am
    from itertools import chain
    myset = set(chain(iterable1, iterable2, iterable3, iterable4))
    

    You get unique items, and you only iterate over each iterable once. chain makes one long iterable from a series of iterables. If you need it sorted, sorted(myset) will give you a sorted list.

    Your Student class needs to implement a __hash__ that is compatible with it’s __eq__:

    def __hash__(self):
        return (self.name, self.ID, self.age).__hash__()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an custom binding defined as follows: <customBinding> <binding name=binaryHttpBinding> <binaryMessageEncoding /> <httpTransport
I have a small custom object defined as: public class TimeSeriesDefinition { public int
I have an object which will be stored in two different data structures. I
I have a custom object called Occasion defined as follows: #import <Foundation/Foundation.h> @interface Occasion
I have a listView with custom objects defined by the xml-layout below. I want
I have defined a custom Sharepoint list for special attributes related to a software
I have defined a custom list template type for SharePoint . I install it
I have defined a custom IPrincipal and custom IIdentity based on a website that
I have defined a custom file type with these lines: syn region SubSubtitle start=+=+
I've defined a custom content type - news. I also have a menu item

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.