learnbyexample@programming.dev to Python@programming.devEnglish · 4 months agoA Guide to Python Lambda Functionsadamj.euexternal-linkmessage-square8fedilinkarrow-up134arrow-down10
arrow-up134arrow-down1external-linkA Guide to Python Lambda Functionsadamj.eulearnbyexample@programming.dev to Python@programming.devEnglish · 4 months agomessage-square8fedilink
minus-squaretunetardis@lemmy.calinkfedilinkEnglisharrow-up2·4 months agoActually, now that I think of it, there’s no reason you need to join the 2 names into a single str. You could just leave it as a tuple of last, first and Python will know what to do in comparing them. >>> sorted(student_ids, key = lambda i: ((rec := student_recs[i])['last'], rec['first'])) [632453, 1261456, 532153] So the lambda would be returning ('Potter', 'Harry') rather than 'Potter, Harry'. But whatever. The := part is still the same.
Actually, now that I think of it, there’s no reason you need to join the 2 names into a single
str
. You could just leave it as atuple
of last, first and Python will know what to do in comparing them.So the lambda would be returning
('Potter', 'Harry')
rather than'Potter, Harry'
. But whatever. The:=
part is still the same.