Just Tech Me At
March 24, 2023
The set data structure is a structure that allows you to store and manipulate collections of unique elements. Unlike lists and tuples, sets do not allow duplicate values and their elements are unordered. Sets also allow you to use various operations such as union, intersection, and difference. In terms of syntax, sets are defined using curly braces {} or the set() constructor. Note that the syntax is similar to that of Python dictionary creation except that dictionaries use key-value pairs. Both structures use curly braces for structure creation. This article will touch on set fundamentals: how to create and manipulate them. Whether you are a beginner or an experienced Python developer, understanding sets can be instrumental when working with collections of unique elements.
You can create a set in two ways. You can either:
The example below uses the first option of manually specifying the elements of the set and enclosing them in curly braces.
mySet = { 'Florida',
'Illinois',
'Pennsylvania',
'Ohio',
'Georgia',
'North Carolina',
'Michigan',
'Virginia',
'Washington',
'Massachusetts',
'Arizona',
'Indiana',
'Tennessee',
'Missouri',
'Maryland',
'Wisconsin',
'Minnesota',
'Colorado',
'Alabama',
'South Carolina',
'Louisiana',
'Kentucky',
'Oregon',
'Oklahoma',
'Connecticut',
'Iowa',
'Arkansas',
'Mississippi',
'Utah',
'Kansas',
'Nevada',
'New Mexico',
'Nebraska',
'West Virginia',
'Idaho',
'Hawaii',
'Maine',
'New Hampshire',
'Rhode Island',
'Montana',
'Delaware',
'South Dakota',
'North Dakota',
'Alaska',
'Vermont',
'Wyoming',
'New Jersey',
'New York',
'California',
'Texas'}
Once a set is created, you can add or remove elements using the add() and remove() methods.
The following syntax is used to add an element to a set.
mySet.add('DummyState')
The following syntax is used to remove an element from a set.
mySet.remove('DummyState')
Unlike lists and tuples, you cannot use indexing to access an element in a set. For sets, accessing elements require a loop.
for x in mySet:
print(x)
You can perform set operations such as union, intersection, and difference on your newly created sets. Operators ( |, &, - ) are used to accomplish this task.
Start by Creating Two Sets
set1 = {'DummyState1','DummyState2','DummyState3','DummyState3'}
set2 = {'Hawaii','Alaska','DummyState1','Florida'}
Perform Union Operation
union = set1 | set2 # a print of the object union returns {'DummyState1','DummyState2','DummyState3','DummyState3','Hawaii','Alaska','DummyState1','Florida'}
Perform Intersection Operation
intersection = set1 & set2 # a print of the object intersection returns {'DummyState1'}
Perform Difference Operation
difference = set1 - set2 # a print of the object difference returns {'DummyState2','DummyState3','DummyState3'}
Sets allow you to efficiently work with unique elements. They offer a range of useful operations for working with such collections. Both the structure and its available operations come in handy when performing set operations and testing for membership. By understanding how to create and manipulate sets you can take advantage of this advanced data structure in your programming projects. Sets are a great tool to have in your programming arsenal.
For more articles on Python data structures, see the following:
For a more advanced review of Python Sets, see the following resources:
Visit Educative.io
Polish Your Python Skills. Visit Tutorials Point!
Visit DataCamp Today!