del and sets proposal

Larry Bates larry.bates at vitalEsafe.com
Thu Oct 2 23:27:04 EDT 2008


Chris Hebert wrote:
> On Thu, Oct 2, 2008 at 3:20 PM, Larry Bates <larry.bates at vitalesafe.com> wrote:
>> You can do the following:
>>
>> a = [1,2,3,4,5]
>> del a[0]
>>
>> and
>>
>> a = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5'}
>> del a[1]
>>
>> why doesn't it work the same for sets (particularly since sets are based on
>> a dictionary)?
>>
>> a = set([1,2,3,4,5])
>> del a[1]
> 
> Sets don't support subscripting, so if you can't go
> 'a_set[something]', why would you expect to be able to be able to
> 'del' such an expression? What would the subscription even mean
> without the 'del'? It doesn't make sense and would just be
> inconsistent.
> 
>> Yes I know that sets have a remove method (like lists), but since
>> dictionaries don't have a remove method, shouldn't sets behave like more
>> like dictionaries and less like lists?  IMHO del for sets is quite
> 
> No, sets are a datatype unto themselves. They are based on
> dictionaries internally (at least in CPython), but that's an
> implemention detail to be hidden, not emphasized.
> 
>> intuitive.  I guess it is too late to change now.
> 
> Most likely.
> 
> Cheers,
> Chris

I believe that the fact that Sets don't support indexing is an implementation 
artifact that is due to the FACT that their underlying implementation is based 
on dictionaries (a good choice in my opinion).  One could just as easily have 
implemented (but then they would not have performed so well) Sets as an ordered 
list of unique values and have provided indexing capabilities (but I can't quite 
think of a use case for such an animal right now).

I didn't mean to imply that del a[1] would delete the first thing in the set, 
but rather the item with a value of 1.  Just as when we use it on a dictionary:

del a[1]

doesn't mean delete the first dictionary entry but rather delete the entry in 
the object with a value of 1, which IMHO would be perfectly logical for a set 
(which is why I started this discussion).  There is no ambiguity because sets, 
like dictionaries, require uniqueness.

Maybe dictionaries should have had a .remove method then things would be more 
consistent?

-Larry



More information about the Python-list mailing list