The Set Constructor Function10:38 with AJ Tran
The Set Constructor function can convert iterable data types into a set. Iterables include strings, lists, tuples, and ranges.
The Set Constructor Function
set() # new empty set set(iterable) # new set from iterable # examples even_numbers = set([2, 4, 6, 8]) odd_numbers = set(range(1, 10, 2))
Some iterable types
Dictionaries and Sets
Passing a dictionary into the set constructor function returns a set of the dictionary keys only.
Dictionaries themselves have methods, such as
.keys(), that return "dict view objects." Dict view objects are often described as "set-like."
Dictionary keys, for example, are already unique and unordered, just like sets. Because they are "set-like," dict view objects can actually use some set operations!
We haven't learned those operations yet (we will soon!) and performing them on dict view objects is beyond the scope of this workshop. I encourage curious minds to try experimenting with them after completing this workshop :)
You need to sign up for Treehouse in order to download course files.Sign up