469,923 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Sets Module Bug?

>>> from sets import *
Set((1,2,3)) Set([1, 2, 3]) Set([1,2,3]) Set([1, 2, 3]) Set(([1,2,3],[4,5,6]))

Traceback (most recent call last):
File "<input>", line 1, in ?
File "sets.py", line 399, in __init__
self._update(iterable)
File "sets.py", line 344, in _update
data[element] = value
TypeError: list objects are unhashable

How can a Set using a tuple or list be ok, but not a tuple of lists?
Jul 18 '05 #1
3 1455
Chris S. wrote:
>>> from sets import *
>>> Set((1,2,3)) Set([1, 2, 3]) >>> Set([1,2,3]) Set([1, 2, 3]) >>> Set(([1,2,3],[4,5,6]))

Traceback (most recent call last):
File "<input>", line 1, in ?
File "sets.py", line 399, in __init__
self._update(iterable)
File "sets.py", line 344, in _update
data[element] = value
TypeError: list objects are unhashable

How can a Set using a tuple or list be ok, but not a tuple of lists?


Because "list objects are unhashable", as Python told you :-)
AFAIK the sets module is built upon the dict type, it uses (requires)
the elements of the set to be hashable for efficient containment tests.
The library reference of the sets module also says:
"All of the elements in iterable should be immutable or be transformable to an immutable
using the protocol described in section 5.13.3. "

--Irmen
Jul 18 '05 #2
Dnia Mon, 02 Aug 2004 05:25:50 -0400, Chris S. napisał(a):
How can a Set using a tuple or list be ok, but not a tuple of lists?


Because tuples can't be changed once they're constructed (= immutable.)
You'd get the same error if you tried to do
d = {[1,2]: 'foo'}


Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
d = {[1,2]: 'foo'}
TypeError: list objects are unhashable

--
Marek Baczyński :: UIN 57114871 :: GG 161671 :: JID im******@jabber.gda.pl
He who knows best best knows how little he knows. -- Thomas Jefferson
Jul 18 '05 #3
> >>> Set((1,2,3))
[...]
How can a Set using a tuple or list be ok, but not a tuple of lists?


I think you are confused by the syntax above. This expression constructs
a Set object, and fills it with the members of the tuple. In no way does
the Set "use" the tuple - once the Set is created, the tuple goes away.

So you should have asked "How can a Set containing numbers be ok, but
not a Set containing lists?", to which the other two posters gave the
right answer.

Regards,
Martin
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Raymond Hettinger | last post: by
5 posts views Thread by Raymond Hettinger | last post: by
1 post views Thread by Gerrit Holl | last post: by
reply views Thread by Dave Benjamin | last post: by
7 posts views Thread by Steve | last post: by
6 posts views Thread by kpp9c | last post: by
17 posts views Thread by Larry Bates | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.