Python Practice › Set Programs › Program 45
Set of favourite actors
Write a Python program to create a set of your favourite actors.
Output
4 ['Akshay Kumar', 'Robert Pattinson', 'Shah Rukh Khan', 'Tom Hardy']
Approach
- A set of strings; print it sorted.
Solution
actors = {"Shah Rukh Khan", "Akshay Kumar", "Tom Hardy", "Robert Pattinson", "Akshay Kumar"}print(len(actors), sorted(actors))Output
4 ['Akshay Kumar', 'Robert Pattinson', 'Shah Rukh Khan', 'Tom Hardy']
Line by line
Line 1
“Akshay Kumar” appears twice but is stored once.
Learn the concept
New to this topic? Read the lesson, then come back.