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 trial

Python

Emily Senechal
PLUS
Emily Senechal
Courses Plus Student 1,384 Points

set()?

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()
Steven Parker
Steven Parker
229,670 Points

Can you provide a link to the course page you were working with?

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The 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
Steven Parker
229,670 Points

It looks like Chris already has you covered. :wink:

Emily Senechal
PLUS
Emily Senechal
Courses Plus Student 1,384 Points

Chris,

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
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.