Question

In: Computer Science

['Mary', 'Anna', 'Emma', 'Elizabeth', 'Margaret', 'Minnie', 'Clara', 'Bertha', 'Ida', 'Annie', 'Alice'] Task #2: Using the list...

['Mary', 'Anna', 'Emma', 'Elizabeth', 'Margaret', 'Minnie', 'Clara', 'Bertha', 'Ida', 'Annie', 'Alice']

Task #2: Using the list above create two sets (using the set data structure) (2 points)¶ Write code that will take the values from the list created above and add them to two different sets. You must utilize the set data structure to complete this task.

The set criteria is as follows: set1 - Includes customers with a name length smaller than or equal to 5 characters set2

- Includes customers with a name length greater than or equal to 5 characters

Your code should print the following: {'Clara', 'Alice', 'Emma', 'Mary', 'Anna', 'Ida', 'Annie'}

{'Clara', 'Bertha', 'Margaret', 'Elizabeth', 'Alice', 'Minnie', 'Annie'}

Solutions

Expert Solution

Code:

names=['Mary', 'Anna', 'Emma', 'Elizabeth', 'Margaret', 'Minnie', 'Clara', 'Bertha', 'Ida', 'Annie', 'Alice']
#initialized a set
set1=set()
set2=set()
#declared 2 sets
for name in names:#this loop iterated for every name in the names list
if(len(name)<=5):#if name length is less than or equals 5
set1.add(name)#we add to set1
if(len(name)>=5):#if name length is greater than or equals 5
set2.add(name)#we add to set2
print("Set1:",set1)
print("Set2:",set2)
#Here we print boath sets

Output:

Indentation:


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT