Adding Elements to a Set

Tags: addition, Set

Write a Python program to add a member to a set and return the modified set.

Example 1:

Input: {1, 2, 3, 4, 5}, 6 
Output: {1, 2, 3, 4, 5, 6}

Example 2:

Input: {5, 7, 8, 9, 10}, 6 
Output: {5, 6, 7, 8, 9, 10}

Use the add() function to add an element to a set.

def add_to_set(s, el):
    s.add(el)
    return s

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!