From 2cd10e2ff75e4ed869acf9e8ba065e27e0949623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D8=A3=D8=AD=D9=85=D8=AF=20=D8=A7=D9=84=D9=85=D8=AD=D9=85?= =?UTF-8?q?=D9=88=D8=AF=D9=8A=20=28Ahmed=20El-Mahmoudy=29?= Date: Sat, 28 Feb 2026 03:56:58 +0100 Subject: [PATCH] Fix sorting of shells & users * The return value didn't sort the shells, sorted(shells.list,key=len) does nothing, as the length of each tuple is 2: the key & the value, while you obviously wanted the length of the user list * The exercise also requested listing the usernames '(sorted alphabetically)' --- ch03-lists-tuples/e12b3_shells_and_users.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ch03-lists-tuples/e12b3_shells_and_users.py b/ch03-lists-tuples/e12b3_shells_and_users.py index 249fad5..be9c2a4 100755 --- a/ch03-lists-tuples/e12b3_shells_and_users.py +++ b/ch03-lists-tuples/e12b3_shells_and_users.py @@ -2,7 +2,7 @@ """Solution to chapter 3, exercise 12, beyond 3: shells_and_users""" from collections import defaultdict - +import operator def shells_and_users_by_popularity(filename): shells = defaultdict(list) @@ -14,4 +14,8 @@ def shells_and_users_by_popularity(filename): shells[shell].append(username) - return sorted(shells.items(), key=len) + # sort usernames for each shell: + for shell in shells: + shells[shell] = sorted(shells[shell]) + + return sorted(shells.items(), key=lambda s: len(s[1]), reverse=True)