Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Sets!
You have completed Python Sets!
Sets are mutable containers. If an element is not already in a set, it can be added to the set at any time.
Adding Elements into a Set
Use .add()
to add single items to a set.
Use .update()
to add many items from an iterable into a set.
These two methods will have no effect if an element is already a member of the set
secondary_colors = set()
secondary_colors.add('orange')
secondary_colors.add('purple')
secondary_colors.add('green')
secondary_colors.add('green')
secondary_colors.add('green')
print(secondary_colors)
# {'green', 'orange', 'purple'}
fancy_colors = set()
fancy_colors.update(['aquamarine', 'emerald green', 'tiffany blue'])
fancy_colors.update(['tiffany blue', 'bacon red'])
print(fancy_colors)
# {'tiffany blue', 'aquamarine', 'bacon red', 'emerald green'}
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