Working with Sets in Python: Unordered Collection of Unique Elements

thumb_up 1  ·  sell Python sets, Set manipulation in Python, Managing unique elements in Python

A set is one of the built-in data types in Python. In mathematics, set is a collection of distinct objects. Set data type is Python's implementation of a set. Objects in a set can be of any data type.

Set in Python also a collection data type such as list or tuple. However, it is not an ordered collection, i.e., items in a set or not accessible by its positional index. A set object is a collection of one or more immutable objects enclosed within curly brackets {}.

Example 1

Some examples of set objects are given below −

 
s1 = {"Rohan", "Physics", 21, 69.75} s2 = {1, 2, 3, 4, 5} s3 = {"a", "b", "c", "d"} s4 = {25.50, True, -55, 1+2j} print (s1) print (s2) print (s3) print (s4)

It will produce the following output −

{'Physics', 21, 'Rohan', 69.75}
{1, 2, 3, 4, 5}
{'a', 'd', 'c', 'b'}
{25.5, -55, True, (1+2j)}

The above result shows that the order of objects in the assignment is not necessarily retained in the set object. This is because Python optimizes the structure of set for set operations.

In addition to the literal representation of set (keeping the items inside curly brackets), Python's built-in set() function also constructs set object.

set() Function

set() is one of the built-in functions. It takes any sequence object (list, tuple or string) as argument and returns a set object

Syntax

Obj = set(sequence)

Parameters

  • sequence − An object of list, tuple or str type

Return value

The set() function returns a set object from the sequence, discarding the repeated elements in it.

Example 2

 
L1 = ["Rohan", "Physics", 21, 69.75] s1 = set(L1) T1 = (1, 2, 3, 4, 5) s2 = set(T1) string = "TutorialsPoint" s3 = set(string) print (s1) print (s2) print (s3)

It will produce the following output −

{'Rohan', 69.75, 21, 'Physics'}
{1, 2, 3, 4, 5}
{'u', 'a', 'o', 'n', 'r', 's', 'T', 'P', 'i', 't', 'l'}

Example 3

Set is a collection of distinct objects. Even if you repeat an object in the collection, only one copy is retained in it.

 
s2 = {1, 2, 3, 4, 5, 3,0, 1, 9} s3 = {"a", "b", "c", "d", "b", "e", "a"} print (s2) print (s3)

It will produce the following output −

{0, 1, 2, 3, 4, 5, 9}
{'a', 'b', 'd', 'c', 'e'}

Example 4

Only immutable objects can be used to form a set object. Any number type, string and tuple is allowed, but you cannot put a list or a dictionary in a set.

 
s1 = {1, 2, [3, 4, 5], 3,0, 1, 9} print (s1) s2 = {"Rohan", {"phy":50}} print (s2)

It will produce the following output −

   s1 = {1, 2, [3, 4, 5], 3,0, 1, 9}
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
   s2 = {"Rohan", {"phy":50}}
        ^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'dict'

Python raises TypeError with a message unhashable types 'list' or 'dict'. Hashing generates a unique number for an immutable item that enables quick search inside computer's memory. Python has built-in hash() function. This function is not supported by list or dictionary.

Even though mutable objects are not stored in a set, set itself is a mutable object. Python has a special operators to work with sets, and there are different methods in set class to perform add, remove, update operations on elements of a set object.

 

 

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back