Introduction
So far, we've looked at the Object Oriented Programming (OOP) side of Python--turns out, we've been using objects for a long time without even realizing it.
A list, for instance, is an object, with its class-specific methods like append( ) or insert( ).
Today, we'll take a look at a similar object (/ data type) that operates in parallel to how lists do--sets.
What Are Sets?
Well, in math, you may know of sets to be a collection of distinct (non-repeating) values. This general notion carries over to Python, as well.
Sets:
store unordered values
unlike lists, we can't index into them
store completely distinct values
no dupes
can store different data types
yes, unlike lists, sets can hold multiple types
are mutable
unlike strings, we can add or remove elements from sets
however, since they're unordered, we can't change existing values
so this wouldn't work simply because indexes don't exist...
myset = set([0, 1.0, 'a', 2])
myset[0] = 1
Initializing Sets
So there are two ways to create or initialize a set.
The first is using the set( ) object itself:
def main():
# notice how we pass values inside of square brackets to the object
myset = set([0, 1.0, 'a', 2])
print(myset)
main()
The second, and more shorthand way:
def main():
myset = {0, 1.0, 'a', 2}
print(myset)
main()
Both produce the exact same output, so it just comes down to preference:
{0, 1.0, 'a', 2}
Car Brands
Let's take an example in which sets might be the most practical choice of "storage," tracking all unique brands of cars.
# list of dictionaries storing car models and their brands
cars = [
{"model": "Civic", "brand": "Honda"},
{"model": "Camry", "brand": "Toyota"},
{"model": "Model S", "brand": "Tesla"},
{"model": "Accord", "brand": "Honda"}
]
# creating a list of distinct car brands
brands = []
for car in cars:
if car["brand"] not in brands:
brands.append(car["brand"])
So, using a list object, this technically works... but we can make some improvements using our set object.
# list of dictionaries storing car models and their brands
cars = [
{"model": "Civic", "brand": "Honda"},
{"model": "Camry", "brand": "Toyota"},
{"model": "Model S", "brand": "Tesla"},
{"model": "Accord", "brand": "Honda"}
]
# creating a set of car brands
brands = set()
for car in cars:
brands.add(car["brand"])
Much cleaner. Using a set, we've automatically filtered out any duplicates by the very nature of the type itself.
And, like append( ) that we use with lists, sets use an instance method called add( ) (pretty self-explanatory.)
Final Thoughts
This was everything you need to know about sets--thanks for reading!
Comments