Clicky

Basic Python Programming Code Snippets: Sets


Basic Python Programming Code Snippets:

Set Data Structure

March 24, 2023

Basic Python

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.

Create a Set

You can create a set in two ways. You can either:

  • assign your variable to a specified group of elements enclosing them with curly braces.
    • variable = {x, y, z}

    or;

  • assign your variable to a list data structure and use the set() constructor which takes the list as a parameter.
    • variable = set([x, y, z])
    • variable = set(myList) # myList is a list of U.S. States. See the article on List Data Structure

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'}

Add/Remove Elements

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')

Accessing Elements of a Set

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)

Union, Intersection, and Difference

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'}

Conclusion

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:



Learn to Code with Educative.io

Visit Educative.io

Educative

Tutorials Point Offers Python Programming Courses

Polish Your Python Skills. Visit Tutorials Point!

Tutorials Point


Integrate Data Science into Your Learning Experience with DataCamp

Visit DataCamp Today!