0

I have an arbitrary number of objects created from this class:

class Person:
    def __init__(self, name, email):
        self.name = name
        self.email = email

I have a list of these objects:

myList = []
JohnDoe = Person("John Doe", "[email protected]")
BobbyMcfry = Person("Bobby Mcfry", "[email protected]")
WardWilkens = Person("Ward Wilkens", "[email protected]")
myList.append(JohnDoe)
myList.append(BobbyMcfry)
myList.append(WardWilkens)

I am wanting to check if someone exists, and if so, return their attributes - if not, say so:

x = input("Who to check for? ")
for i in myList:
    if i.name == x:
        print("Name: {0}\nEmail: {1}".format(i.name, i.email))
    else:
        print("{0} is not on the manifest.".format(x))

This kind of works, but returns one or the other for each Person in myList - I only want one return...

I realize I need to do some sort of

if val in myList:....

But I'm having trouble how to word what "val" should be without iterating through each object

1
  • 1
    You could add a break and make your loop greedy and stop on the first match. Commented Dec 20, 2012 at 23:07

2 Answers 2

5

Using the loop is fine, you just need to handle the case where none of the names matched, you can do this easily using break and else:

x = input("Who to check for? ")
for i in myList:
    if i.name == x:
        print("Name: {0}\nEmail: {1}".format(i.name, i.email))
        break
else:
    # this is only run if 'break' was not executed inside of the loop
    print("{0} is not on the manifest.".format(x))

Depending on what you use the list for, you might be better of using a dictionary to link names to Person objects:

myDict = {}
JohnDoe = Person("John Doe", "[email protected]")
BobbyMcfry = Person("Bobby Mcfry", "[email protected]")
WardWilkens = Person("Ward Wilkens", "[email protected]")
for person in [JohnDoe, BobbyMcfry, WardWilkens]:
    myDict[person.name] = person

x = input("Who to check for? ")
person = myDict.get(x)
if person:
    print("Name: {0}\nEmail: {1}".format(person.name, person.email))
else:
    print("{0} is not on the manifest.".format(x))
Sign up to request clarification or add additional context in comments.

3 Comments

Worked perfectly. I didn't realize the else would work with anything other than a linked "if" - nice to know.
If you are going to make a dict accessor to the person list, rather than duplicating the names, either make a function that creates and inserts in to the dict using a parameterized name that you only hardcode once, or wrap the collection in an object that overrides __getitem__ to retrieve a person with a name matching the provided key.
Appreciate the dictionary example also - I was earlier trying to link names to the objects but did not know how.
1

Could use itertools.

try:
    found_person = itertools.dropwhile(lambda person: person.name != search_name, people).next()
except StopIteration:
    found_person = None

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.