How to print just the names in a tuple [closed]

This can be done in many ways, but perhaps the simplest and most concise is using list comprehensions.

E.g.

def old_enough(students):
    return [name for name, age in students if age > 17]

Which can be less concisely expressed using a for loop as:

def old_enough(students):
    voters = []
    for name, age in students:
        if age > 17:
            voters.append(name)
    return voters

My preference is for the list comprehension, I think it is more pythonic and readable.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top