Python Practice › Set Programs › Program 48

Longest word in a set

EasySet Programs · 48 of 48

Write a Python program to find the longest word in a set.

Advertisement
Input
{"cat", "elephant", "dog", "hippopotamus"}
Output
hippopotamus

Approach

  1. max(s, key=len).

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment