Basics of set and creating a set in Python

By Abhinash Jena & Priya Chetty on August 28, 2022

Previous articles have discussed the Python syntax and built-in functions for handling the string and list data types, 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 like lists but unlike lists, sets contain unique values in an unordered sequence.

NOTE

List sets are also mutable, but the elements are immutable like Booleans, strings, 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')
print(f)
{'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)
print(s)
{'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)
print(set)
{'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)
print(set)
#OUTPUT
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)
print(s)
frozenset({'H', 'P', 'Y', 'A'})
NOTES

I am an interdisciplinary educator, researcher, and technologist with over a decade of experience in applied coding, educational design, and research mentorship in fields spanning management, marketing, behavioral science, machine learning, and natural language processing. I specialize in simplifying complex topics such as sentiment analysis, adaptive assessments and data visualizatiion. My training approach emphasizes real-world application, clear interpretation of results and the integration of data mining, processing, and modeling techniques to drive informed strategies across academic and industry domains.

I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them. 

Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here

I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal. 

 

Discuss