Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for How can I change my longest and shortest functions to avoid repeating code in Python?

$
0
0

The function can be simplified by iterating using a for loop.

def find_shortest(words):    shortest = words[0]    for word in words:        if len(shortest) >= len(word):            shortest = word    return shortest

At which point trying to reuse such trivial code leads to more complicated and slower code; sometimes cut & paste is fine.


Viewing all articles
Browse latest Browse all 581

Trending Articles