fromcollectionsimportdefaultdictmessages=defaultdict(lambda:'%s, %s and %d others like this',{0:'no one likes this',1:'%s like this',2:'%s and %s like this',3:'%s, %s and %s like this',})likes_quantity=len(persons)content=messages[likes_quantity]
But of course - all of this is redundant, since you already check for likes_quantity > 3. So how about this:
defshow_likes(persons:List[str])->str:messages={0:'no one likes this',1:'%s like this',2:'%s and %s like this',3:'%s, %s and %s like this',}likes_quantity=len(persons)iflikes_quantity>3:return'%s, %s and %d others like this'%(persons[0],persons[1],len(persons[2:]))returnmessages[likes_quantity]%tuple(persons)
Possible improvements: instead of
You can write:
Or - if you want to get fancy:
Or even:
But of course - all of this is redundant, since you already check for
likes_quantity > 3
. So how about this:Nice, it was in fact redundant getting the message content, thanks for sharing :D