Skip to content

Commit e315133

Browse files
iansedanogahjelle
andauthored
changing nested sort to use get (#280)
* changing nested sort to use get * simplify return Co-authored-by: Geir Arne Hjelle <[email protected]>
1 parent 5a225f4 commit e315133

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

sort-python-dictionary/nested_sort.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,23 @@
1111
}
1212

1313

14-
def get_python(item):
14+
def get_relevant_skills(item):
15+
"""Get the sum of Python and JavaScript skill"""
1516
skills = item[1]["skills"]
16-
if "python" in skills:
17-
return skills["python"]
18-
else:
19-
# Return value that is equivalent to having no Python skill
20-
return 0
2117

18+
# Return default value that is equivalent to no skill
19+
return skills.get("python", 0) + skills.get("js", 0)
2220

23-
print(sorted(data.items(), key=get_python, reverse=True))
2421

25-
# Doing the same thing with lambda function and conditional expression
22+
print(sorted(data.items(), key=get_relevant_skills, reverse=True))
23+
24+
# Doing the same thing with lambda function
2625
print(
2726
sorted(
2827
data.items(),
29-
key=lambda item: item[1]["skills"]["python"]
30-
if "python" in item[1]["skills"]
31-
else 0,
28+
key=lambda item: (
29+
item[1]["skills"].get("python", 0) + item[1]["skills"].get("js", 0)
30+
),
3231
reverse=True,
3332
)
3433
)

0 commit comments

Comments
 (0)