Adding Item to a Set In Python

Python Set Definition

A set is an unordered collection data type with no duplicate elements. Sets are iterable and mutable. The elements appear in an arbitrary order when sets are iterated.

The elements of the set can not be duplicate. The elements of the python set must be immutable.


Add items to a Set

If we want to add single item to a list, we can use add() method.

If we want to add more than one item to a list, we can use update() method.


Example: using Add() method




Output:

 Printing the element using for loop...
 Anmol
 Rakesh
 Prayag
 Sujeet
 Rahul
 Aimtocode
 Mukesh
 Pankaj

Here, in above example by using add() method and adding one element Aimtocode.


Example : using update() method



Output:

 Printing the element using for loop...
 Sujeet
 tutorial
 Rahul
 Python
 Mukesh
 Prayag
 Anmol
 Aimtocode
 Pankaj
 Rakesh

In above example, update() method is used to add multiple elements.