53.5k views
3 votes
Creating and modifying sets. The top 3 most popular male names of 2017 are Oliver, Declan, and Henry according to babynames.com. Write a program that modifies the male_names set by removing a name and adding a different name. Sample output with inputs: 'Oliver' 'Atlas' { 'Atlas', 'Declan', 'Henry' } NOTE: Because sets are unordered, the order in which the names in male_names appear may differ from above. 370918.1946576.qx3zqy7

1 test passed All tests passed Run No solution code provided Testing with inputs: 'Oliver' 'Atlas' Output differs.
See highlights below.
Your output { 'Declan', 'Henry', 'Oliver' } Expected output { 'Atlas', 'Declan', 'Henry' }
Testing with inputs: 'Declan' 'Noah' Output differs.
See highlights below. Your output { 'Declan', 'Henry', 'Oliver' } Expected output { 'Henry', 'Noah', 'Oliver' }
I can't seem to get the right output for {'Henry', 'Noah', 'Oliver'}.

User Ocramius
by
7.3k points

1 Answer

4 votes

Final answer:

To modify a set in Python, use the remove() method to remove a name and the add() method to add a different name.

Step-by-step explanation:

To modify a set in Python, you can use the methods remove() and add(). To remove a name from the male_names set, you can use the remove() method by passing the name as an argument. To add a different name to the set, you can use the add() method by passing the new name as an argument. Here's an example:



male_names = {'Oliver', 'Declan', 'Henry'}

# Remove 'Oliver'
male_names.remove('Oliver')

# Add 'Atlas'
male_names.add('Atlas')

print(male_names)



The output of the above code will be:



{'Atlas', 'Declan', 'Henry'}
User Jschr
by
7.7k points