Python Practice › Set Programs › Program 48
Longest word in a set
Write a Python program to find the longest word in a set.
Input
{"cat", "elephant", "dog", "hippopotamus"}Output
hippopotamus
Approach
max(s, key=len).
Solution
print(max({"cat", "elephant", "dog", "hippopotamus"}, key=len))Output
hippopotamus
Note
If two words share the longest length, the result depends on set order; use max(sorted(s), key=len) to make it predictable.
Learn the concept
New to this topic? Read the lesson, then come back.