Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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
- strings
- lists
- tuples
- ranges
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 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 :)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up