Bec,
I see you already have a reply, including the list comprehension that may be ahead of what you are expected to use now.
I will try another approach and hopefully one that is at a lower level, if that is what you need. If not, then you alredy know enough.
First, perhaps as an artifact, I am getting the wrong type of quotes in the one-liner you sent to populate the file as it was not in the ```python format of the rest of your code.
people = [(‘Jake’, 36, ‘M’),(‘Rose’, 24, ‘F’),(‘Derek’, 78, ‘F’),(‘Alan’, 17, ‘M’),(‘Sarah’, 14, ‘F’)]
I had to replace all the open/close single quotes to get:
people = [('Jake', 36, 'M'),('Rose', 24, 'F'),('Derek', 78, 'F'),('Alan', 17, 'M'),('Sarah', 14, 'F')]
It is a list of tuples containing what seems to be a name/age/gender and perhaps could have been written a tad more clearly as:
people = [
('Jake', 36, 'M'),
('Rose', 24, 'F'),
('Derek', 78, 'F'),
('Alan', 17, 'M'),
('Sarah', 14, 'F')]
The above is not important but it helps to see the internal structure so a different approach is possible.
If you loop over this, you can do what you did and get one tuple at a time and then use something like entry[1] to refer to the age in what is the second part of each tuple, but just for the heck of it, consider this code in which you deconstruct each tuple:
def adults(members):
people = []
for name, age, gender in members:
if age >= 18:
people.append(name)
return people
I am assuming from your code that you only want the names, not the full tuples. When I run the code, I get this result:
>>> adults(people)
['Jake', 'Rose', 'Derek']
Note that if you wanted to return the entire tuples that matched, use this instead:
def adults_tup(members):
people = []
for name, age, gender in members:
if age >= 18:
people.append((name, age, gender))
return people
This version returns a subset of the list:
>>> adults_tup(people)
[('Jake', 36, 'M'), ('Rose', 24, 'F'), ('Derek', 78, 'F')]
If you really don’t want the gender, some people would replace it with a single underscore to mean you don’t care.
The above is not a better method but just an alternate to what Alex showed. He went with your method of using subscripting and that is fine. The method I am showing may be more readable to some, or more confusing if they do not understand how python supports multiple assignments as shown.
If you have learned how to use a list comprehension, I supply my variant of what Alex suggests, again, not as better but as another way of looking at it. It only cares about name and age and ignores gender and is really internally seen as automatically building a list and returning it in a more compressed and hopefully readable way:
def adults(members):
return [name for name, age, _ in members if age >= 18]
But note that this function is so short as a one-liner that it may not be needed at all if you use it only once. You could just have:
>>> old_enough = [name for name, age, _ in people if age >= 18]
>>> old_enough
['Jake', 'Rose', 'Derek']
>>> [name for name, age, _ in people if age < 18]
['Alan', 'Sarah']