Basics of set and creating a set in Python

By Abhinash & Priya Chetty on August 28, 2022

Previous articles have discussed the Python syntax and in-built functions for handling the string and list data type, both of which are sequence-type data. This article focuses on another sequence type data, the set and Frozenset and creating a set and frozenset. Sets are similar to lists but unlike lists, sets contain unique values in an unordered sequence.


Like list sets are also mutable but the elements are immutable like boolean, string, integers and tuples.

Since sets are mutable, they cannot be elements of another set. In the case of Frozensets, they are immutable sets such that they cannot be changed once created.

x = {1, 2, 3, 4}
y = {'a', 'b', 'c'}

Creating a set using set()

Apart from declaring a set, they can also be created by converting various data types into a set, using the set() function.

String to set

When a string is converted to a set, each character becomes an element of the set. Multiple occurrences of an alphabet are omitted from the set.

f = set('Happy')
{'H', 'p', 'a', 'y'}

List to set

Python list can also be changed to a set using the set function. In this case, too the multiple occurrences of the same value will be omitted.

f = ['H','A','P','P','Y']
s = set(f)
{'H', 'P', 'Y', 'A'}

Similarly, a set can also be created from a list with integers and boolean values.

Tuple to set

Tuples are another type of sequence data in Python, in which the elements are immutable. These can also be converted to sets using the set()function.

seasons = ('winter','spring','summer','autumn')
set = set(seasons)
{'summer', 'autumn', 'spring', 'winter'}

Frozen sets

Another form of set, the Frozenset are essentially a mutable form of sets. Therefore, once Frozensets are created, they cannot be changed. Frozensets in Python are created using the frozenset() function.

seasons = ('winter','spring','summer','autumn')
set = frozenset(seasons)
frozenset({'summer', 'autumn', 'spring', 'winter'})

Frozensets can also be created from other types of sequence data.

f = ['H','A','P','P','Y']
s = frozenset(f)
frozenset({'H', 'P', 'Y', 'A'})

Priya is the co-founder and Managing Partner of Project Guru, a research and analytics firm based in Gurgaon. She is responsible for the human resource planning and operations functions. Her expertise in analytics has been used in a number of service-based industries like education and financial services.

Her foundational educational is from St. Xaviers High School (Mumbai). She also holds MBA degree in Marketing and Finance from the Indian Institute of Planning and Management, Delhi (2008).

Some of the notable projects she has worked on include:

  • Using systems thinking to improve sustainability in operations: A study carried out in Malaysia in partnership with Universiti Kuala Lumpur.
  • Assessing customer satisfaction with in-house doctors of Jiva Ayurveda (a project executed for the company)
  • Predicting the potential impact of green hydrogen microgirds (A project executed for the Government of South Africa)

She is a key contributor to the in-house research platform Knowledge Tank.

She currently holds over 300 citations from her contributions to the platform.

She has also been a guest speaker at various institutes such as JIMS (Delhi), BPIT (Delhi), and SVU (Tirupati).


I am currently working as a Research Associate. My work is centered on Macroeconomics with modern econometric approach. Broadly, the methodological research focuses on Panel data and Times series data analysis for causal inference and prediction. I also served as a reviewer to Journals of Taylor & Francis Group, Emerald, Sage.