How to Initialize Set in Python

Spread the love

You can easily initialize the Set in Python either by using the set() function and {}, Let’s discuss these and several ways to initialize a Set. Set in python is a one-dimensional data structure that will not allow duplicate entries. It can be possible to include multiple data type elements.

1. Quick Examples of initializing Set

Following are quick examples that will discuss how to initialize the Set.


# Initialize set using set()
myset1=set()

# Add element to set
myset1.add(100)

# Initialize set with tuple using set()
myset1=set((100,200,300))

# Initialize set with list using set()
myset1=set([300,100,200,300])

# Initialize set using {}
myset1={100,200,300}

# Unpack the list using the Unpacking Operator
list1=[200, 100, 300]
myset1={*list1}

# Unpack the tuple using the Unpacking Operator
tuple1=(200, 100, 300,600,600,800)
myset1={*tuple1}

# Unpack the dictionary values using the Unpacking Operator
scores={'Day 1':200, 'Day 2':100, 'Day 3':300,'Day 4':600,'Day 5':600}
myset1={*scores.values()}

# Unpack the dictionary keys using the Unpacking Operator
myset2={*scores.keys()}

2. Initialize Set using set() function

The set() function is used to initialize/create a set in Python. The set() function is a built-in function that is used to create either an empty set or initialize a set with some default values. We can pass a list/tuple etc. to the set() to initialize a set with elements.

2.1 set() function – Syntax

Following is the syntax of the set() function


# Empty set
myset1=set()

# Initialize set with elements
myset1=set([elements])

2.2 Examples

Example 1: Create an empty set using the set() function. The following example creates an empty set object myset1.


# Initialize set using set()
myset1=set()
print(myset1)

# Output:
# set()

Example 2: Add one element using the set.add() method to the empty set created using the set() function. The following example adds 100 to the set.


# Initialize set using set()
myset1=set()
myset1.add(100)
print(myset1)

# Output:
# {100}

Example 3: Initialize the set with a tuple of elements. The following example takes the tuple as an argument to the set() function and creates a set with values 100, 200, and 300.


# Initialize set using set()
myset1=set((100,200,300))
print(myset1)

# Output:
# {200, 100, 300}

Example 4: Pass List of elements to the set() function. The following example takes the list as an argument to the set() function and creates a set with values 100, 200, and 300.

Related: Convert list to set in Python.


# Initialize set using set()
myset1=set([300,100,200,300])
print(myset1)

# Output:
# {100, 300, 200}

We are passing the list that has one duplicate entry while creating the set using the set() function. As the set won’t allow duplicates, one 300 is excluded from the set. The Set now holds only 3 integers.

3. Initialize the Set using {}

{} is used to initialize or create a Python set with elements like the set() function. It is important to pass the element to this {}, while creating, otherwise it will be considered as a Dictionary.

3.1 Syntax

Let’s see the syntax of creating set using {}.


# Syntax to initialize set using {}
myset1={}

3.2 Example

Create a set that holds three integers using {}. Note that, if you don’t pass values to the {}, it considers the dictionary hence, always pass values while initializing as a set.


# Initialize set using {}
myset1={100,200,300}
print(myset1)

# Output:
# {200, 100, 300}

4. Unpacking the Iterable

In this scenario, we will use the existing iterable like list/tuple/dictionary, etc, and pass this to the set – {}. While passing we need to unpack the iterable by passing the unpacking operator – *.

All elements from the iterable are passed to the set excluding duplicates. Following examples initializes set by using unpacking the Python iterable objects

4.1 Syntax

Following is the syntax of passing the iterable to the set.


# Syntax
myset1={*iterable}

4.2 Examples

Example 1: Consider the list of 3 integers and pass this to the set.


list1=[200, 100, 300]

# Using the Unpacking Operator
myset1={*list1}
print(myset1)

# Output:
# {200, 100, 300}

Example 2: Consider a tuple of 6 integers and pass this to the set.


tuple1=(200, 100, 300,600,600,800)

# Using the Unpacking Operator
myset1={*tuple1}
print(myset1)

# Output:
# {800, 100, 200, 300, 600}

Tuple hold duplicate entry i.e 600. After passing to the set, it is removed and set holds only 5 elements.

Example 3: Consider a Dictionary of 5 items and convert values and keys to set separately.


scores={'Day 1':200, 'Day 2':100, 'Day 3':300,'Day 4':600,'Day 5':600}

# Using the Unpacking Operator 
myset1={*scores.values()}
print(myset1)

myset2={*scores.keys()}
print(myset2)

Here,

  1. dictionary.values() method will return only values from a dictionary. By passing them to the set, only values are converted to the set excluding duplicates.
  2. dictionary.keys() method will return only keys from a dictionary. By passing them to the set, only keys are converted to the set.

5. Conclusion

We have seen how to initialize the Python set using set() and {} approaches. It can also be possible to pass the iterables to the set() function by unpacking them using the unpack operator (*). For each scenario, we discussed simple examples to understand the concept better.

Happy Learning!!

Leave a Reply

You are currently viewing How to Initialize Set in Python