Description

Set is an unordered collection data type which does not contain duplicates elements. A set is iterable and mutable data type.

Syntax of set definition.

s_et = set()
print(type(s_et))

Output:

<class 'set'>

Create a New set

new_set = set({1, 2, 3, 4, 5})
print(new_set)

Output:

{1, 2, 3, 4, 5}

n_set = {1,2,3,}
print(type(n_set))

Output:

<class 'set'>

Add Element to the set.

# Add element to the set
new_set = set({1, 2, 3, 4, 5})
new_set.add("f")
print(new_set)

Output:

{1, 2, 3, 4, 5, 'f'}

Discard Element fromt the set.

# Discard element from the set
new_set = set({1, 2, 3, 4, 5})
new_set.discard(3)
print(new_set)

Output:

{1, 2, 4, 5}

Remove element from the set.

# Remove element from the set
new_set = set({1, 2, 3, 4, 5})
new_set.remove(5)
print(new_set)

Output:

{1, 2, 3, 4}

Discard element from the set is not existing.

# Discard element from the set is not existing
new_set = set({1, 2, 4, 5})
new_set.discard(3)
print(new_set) # Same set as print 

Output:

{1, 2, 4, 5}

Remove element from the set is not existing.

# Remove element from the set in not existing 
new_set = set({1, 2, 4, 5})
new_set.remove(3)
print(new_set) # Return Keyerror

Output:

Traceback (most recent call last):
  File "d:\dir_name\name_file.py", line 3, in <module>
    new_set.remove(3)
KeyError: 3

Pop an Element from the set.

# Pop an element from the set
new_set = set({1, 2, 4, 5})
print(new_set.pop())
print(new_set)

Output:

1
{2, 4, 5}

clear the set in python.

# clear the set
new_set = set({1, 2, 4, 5})
new_set.clear()
print(new_set)

Output:

set()

Iterate the all elements of the set in Python.

# Iterate set elements
new_set = set({1, 2, 4, 5})
for item in new_set:
    print(item)

Output:

1
2
4
5

Python Sets operations.

Set can be used the mathematical set oprations like union, intersection, difference and symmetric differenece. We can perform the all operations of set with methods.

# Create new sets
set1 = {1, 2, 3, 4, 5}
set2 = {2,6, 8,1, 9, 10}

Perfrom Union Operation on set in Python.

# Perform Union Operation

print(set1 | set2)  # Method 1
u_set = set1.union(set2)
print(u_set)

Output:

{1, 2, 3, 4, 5, 6, 8, 9, 10}
{1, 2, 3, 4, 5, 6, 8, 9, 10}

Perfrom Intersection operation on set in python.

# Perform Intersection operation
print(set1 & set2)  # Method 1
u_set = set1.intersection(set2)
print(u_set)

Output:

{1, 2}
{1, 2}

Perform difference operation on sets in python.

# Perform difference operation
print(set1 - set2) # Method 1

diff_set = set1.difference(set2)
print(diff_set)

Output:

{3, 4, 5}
{3, 4, 5}

Perform Symetric Differece Operation.

# Perform Symetric Difference Operation
print(set1 ^ set2)
sym_dif_set = set1.symmetric_difference(set2)
print(sym_dif_set)

Output:

{3, 4, 5, 6, 8, 9, 10}
{3, 4, 5, 6, 8, 9, 10}

Other Python Set Methods

Method Description
add() Adds an element to the set.
clear() Removes all elements from the set.
copy() Returns a copy of the set.
difference() Returns the difference of two or more sets as a new set.
difference_update() Removes all elements of another set fromt this set.
discard() Removes an element from the set if it is a member. (Do nothing if the element is not in set).
intersection() Returns the intersection of two sets as a new set.
intersection_update() Updates the set with intersection of itself and another.
isdisjoint() Returns 'True' if two sets have a null intersection.
issubset() Returns 'True' if another set contains this set.
issuperset() Returns 'True' if this set contains another set.
pop() Removes and returns an arbitrary set element. Raises 'Keyerror' if the set is empty.
remove() Removes an elements from the set. If the element is not a member, raise a 'Keyerror'.
symmetric_difference() Returns the symmetric difference of two sets as a new set.
symmetric_difference_update() Updates a set with the symmetric difference of itself and another.
union() Returns the union of sets in a new set.
update() Updates the set with the union of itself and others.

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Conditional Statement If elif else statement in Python

Like some situation comes in our real life in which we have to take some decision as to what we have to do next.

Everything about of Loops in Python | For loop | While loop | Nested Loop

A loop is used for iterate the sequence. It is used to iterate List, String, Dictionary, Tuple. Iterating over a sequence is called traversal.

Function | Docstring | Lambda | Kwargs in Python

In Python programming, a function is a block of statements that makes our code readable. If we do that function in the program, then we do not have to write the same statement of code again and again.

What is Python? How Python is interpreted? What are the tools that help to find bugs or perform static analysis? What are Python Decorators?

Python Program is a gerneral purpose of high level, intereted, interative and object-oriented scription programming language developed by Guido Van Rossum in  1991.