del and sets proposal

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Oct 4 12:36:28 EDT 2008


Larry Bates a écrit :
> 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]
 >
> 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 
> intuitive.

For lists, del a[x] means 'remove item at index x'. For dicts, del a[x] 
means 'remove key:value pair which key is x'. In both cases, del a[x] is 
meaningful because a[x] is meaningful. Sets are neither ordered nor 
indexed, and can't be subscripted. So "del aset[x]" is IMHO as 
meaningless as 'aset[x]' is. Using this syntax for an operation which 
semantic is the same as alist.remove(x) would be at best inconsistent 
and confusing.




More information about the Python-list mailing list