Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEmily Senechal
Courses Plus Student 1,384 Pointsset()?
I believe I entered the code in line by line. For some reason though I was getting a different answer. I was wondering what set() means and/or I entered my code incorrectly since the video guide got [].
>>> inventory = {"shield", "apples", "sword", "bow", "boomerang"}
>>> for item in inventory.copy():
... inventory.remove(item)
...
>>> inventory
set()
3 Answers
Chris Freeman
Treehouse Moderator 68,423 PointsThe code is behaving correctly:
# define a set
>>> inventory = {"shield", "apples", "sword", "bow", "boomerang"}
# remove each item in the set
>>> for item in inventory.copy():
... inventory.remove(item)
# empty set remains
>>> inventory
set()
Since {} is used to represent an empty dict
, an empty set is represented by set().
set()
is also used to create an empty set.
Since the video ended with [], the empty list
, perhaps you meant to start with:
>>> inventory = ["shield", "apples", "sword", "bow", "boomerang"] # crest list
Post back if you need more help. Good luck!!!
Steven Parker
229,670 PointsIt looks like Chris already has you covered.
Emily Senechal
Courses Plus Student 1,384 PointsChris,
Thank you for your reply and yes that was why it had ended differently. Why is it that different brackets change the output? Thank you for your time and keen attention for detail.
Chris Freeman
Treehouse Moderator 68,423 Points- Using { and } declares a
set
, when empty, it is represented by the__repr__
value of an empty set: set() - Using [ and ] declares a
list
, when empty, it is represented by the__repr__
value of an empty list: []. This could also have been displayed as list(), but [] is the accepted equivalent shorthand notation.
Steven Parker
229,670 PointsSteven Parker
229,670 PointsCan you provide a link to the course page you were working with?