Set add() in python

Our aim in this article is to meet your learning objectives by examining Python sets add with examples.

Syntax: 

set.add( elem )

Parameters:

  • elem: To which a set should be added.


Add Any Iterable

Python sets add allows you to update any iterable object, not just sets (tuples, lists, dictionaries, etc).

You can add items of a list to a set:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Australia"} country_list = ["France", "Ireland"] country_set.update(country_list) print(country_set)
Python sets add examples

Example: 

odd_set = {1, 3, 5, 7, 9} even_list = [10, 12, 14, 16] odd_set.update(even_list) print(odd_set)

Set Add – New Items

In Python sets add, once a set is created, you cannot change its items, but you can add new ones.

Use the add() method to add one item to a set.

Using the add() method, add an item to a set:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Australia"} country_set.add("France") print(country_set)

Add integer items to a set using the add() method:

Example: 

odd_set = {1, 3, 5, 7, 9} odd_set.add(11, 12) print(odd_set)

Sets Add – Update Items

Use the update() method to add items from another set to the current set.

Adding elements from country_set1 to country_set:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Australia"} country_set1 = {"France", "Ireland"} country_set.update(country_set1) print(country_set)

Update integer to odd_set from even_set:

Example: 

odd_set = {1, 3, 5, 7, 9} even_set = {10, 12, 14, 16} odd_set.update(even_set) print(odd_set)

Set add() method Uses

Here are some common use cases for the add() method:

  1. You can use the add() method to add individual elements to a set. If the element is already present in the set, it will be ignored since sets only contain unique elements.
  2. When you don’t know the initial elements of a set and need to build it dynamically, you can start with an empty set and use the add() method to add elements one by one as you encounter them or as they become available.
  3. By starting with an empty set and using the add() method in a loop, you can construct a set from an iterable object like a list, tuple, or string. This allows you to extract unique elements from the iterable and store them in the set.
  4. If you have an existing set and want to add new elements to it, you can use the add() method to append individual elements. This is particularly useful when you have multiple sources of elements and want to combine them into a single set.
  5. The add() method can also be used to check whether an element is already present in the set. If the element is successfully added, it indicates that it was not already present. This can be useful for detecting and processing unique values.
Your engagement matters! Appreciate our efforts or contribute suggestions for improving this site by leaving your reaction below.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *