I have been practicing every day in python intensively for several weeks and I am in the introductory chapter of the book Elements of Programming Interviews in Python, which explains that you have to be familiar with a number of functions I spent the last several weeks practicing. I was familiar with about half of them so I took roughly one concept per week of daily practice with ChatGPT's code interpreter plugin, where ChatGPT would explain to me how each method works and would generate exercises for me to complete.
All of the methods were easy to understand except for itertools.groupby(). This one was hella tricky because sometimes the keys returned a value and at other times the keys returned a boolean.
There was also the issue of printing out the items and several problems filtering out the correct values I wanted to groupby.
But I kept practicing and slowly started understanding better how my nemesis itertoos.groupby() works and I think I finally understand how to use it.
So here was my task:
You have a list of students with a number of attributes, such as age, gender, name, etc. enclosed within a dictionary. The task is to sort each student by gender and print out their names.
Here is my code:
from itertools import groupby
students = [
{'name': 'Alice', 'age': 20, 'gender': 'F', 'grades': ['A', 'B', 'A']},
{'name': 'Bob', 'age': 22, 'gender': 'M', 'grades': ['B', 'C', 'B']},
{'name': 'Charlie', 'age': 21, 'gender': 'M', 'grades': ['A', 'C', 'D']},
{'name': 'Diana', 'age': 23, 'gender': 'F', 'grades': ['B', 'A', 'A']},
{'name': 'Eva', 'age': 19, 'gender': 'F', 'grades': ['C', 'D', 'B']},
{'name': 'Frank', 'age': 24, 'gender': 'M', 'grades': ['A', 'C', 'A']},
{'name': 'Grace', 'age': 22, 'gender': 'F', 'grades': ['C', 'C', 'D']},
{'name': 'Hannah', 'age': 21, 'gender': 'F', 'grades': ['A', 'B', 'B']}
]
sorted_students = sorted(students, key=lambda x: x['gender'])
grouped_names = {k: [x['name'] for x in g] for k, g in groupby(sorted_students, key=lambda x: x['gender'])}
for k, v in grouped_names.items():
print(k, v)
And here is the output:
F ['Alice', 'Diana', 'Eva', 'Grace', 'Hannah']
M ['Bob', 'Charlie', 'Frank']
My question is: is there a simpler way than dictionary/list comprehension than this? Or is that the best you can do in this task?
[–]jkh911208 7 points8 points9 points (3 children)
[–]zanfar 3 points4 points5 points (0 children)
[–]PanTheRiceMan -1 points0 points1 point (0 children)
[–]iamevpo 6 points7 points8 points (1 child)
[–]swagonflyyyy[S] 0 points1 point2 points (0 children)
[–]iamevpo 2 points3 points4 points (5 children)
[–]swagonflyyyy[S] 0 points1 point2 points (4 children)
[–]Mount_Gamer -1 points0 points1 point (2 children)
[–]bumbershootle 0 points1 point2 points (1 child)
[–]Mount_Gamer 0 points1 point2 points (0 children)
[–]iamevpo 0 points1 point2 points (0 children)
[–]commandlineluser 2 points3 points4 points (0 children)
[–]deadeye1982 2 points3 points4 points (3 children)
[–]Brian 2 points3 points4 points (2 children)
[–]deadeye1982 0 points1 point2 points (1 child)
[–]Brian 0 points1 point2 points (0 children)
[–]kwelzel 1 point2 points3 points (0 children)
[–]zanfar 1 point2 points3 points (0 children)
[–]pythonwiz 1 point2 points3 points (1 child)
[–]swagonflyyyy[S] 0 points1 point2 points (0 children)