Search⌘ K
AI Features

Solution: Separate the Names in a Set into Two Sets

Explore how to separate names stored in a Python set into two distinct sets based on the initial letter. Learn to use loops and conditional checks for set operations to categorize elements efficiently.

We'll cover the following...

The solution to the problem of separating the names given in a set into two sets is given below.

Solution

Python 3.8
# Split given set into two sets
st = {'Amelia', 'Ava', 'Alexander', 'Avery', 'Asher', 'Bam', 'Bob', 'Bali', 'Bela', 'Boni'}
t = set( )
s = set( )
for item in st :
if item.startswith('A') :
t.add(item)
elif item.startswith('B') :
s.add(item)
print(t)
print(s)
...