Velocity Reviews > Sets in Python

# Sets in Python

sapsi
Guest
Posts: n/a

 09-19-2007
Hello,
I recently tried using the set function in Python and was surprised to
find that

a=[ 1, 2,3, [1,2] ]

doesn't work with 'set', throwing TyperError (unhashable exception). I
found out that this is because lists can't be hashed.

So,this implies 'a' cannot be a set in python which i think is quite
unfortunate, after all 'a' does look like a mathematical set.

My question is,
1) Why can't lists be hashed?
and
2) This is not related, but is there i neat way (without pop and list
comprehension) to convert a set into a list? I say neat because i'm
guessing using list comprehension might turn out be slow and there
might be other methods which are faster.

SM

Evil Bert
Guest
Posts: n/a

 09-19-2007
sapsi wrote:
> 2) This is not related, but is there i neat way (without pop and list
> comprehension) to convert a set into a list? I say neat because i'm
> guessing using list comprehension might turn out be slow and there
> might be other methods which are faster.

a = set([1, 2, 3, 4])
b = list(a)

Raymond Hettinger
Guest
Posts: n/a

 09-19-2007
On Sep 18, 5:39 pm, sapsi <(E-Mail Removed)> wrote:
> I recently tried using the set function in Python and was surprised to
> find that
>
> a=[ 1, 2,3, [1,2] ]
>
> doesn't work with 'set', throwing TyperError (unhashable exception). I
> found out that this is because lists can't be hashed.
> So,this implies 'a' cannot be a set in python which i think is quite
> unfortunate, after all 'a' does look like a mathematical set.

This is written as:

a = set([1, 2, 3, frozenset([1, 2])])

> This is not related, but is there i neat way (without pop and list
> comprehension) to convert a set into a list?

list(a)

Raymond

Asun Friere
Guest
Posts: n/a

 09-19-2007
On Sep 19, 10:39 am, sapsi <(E-Mail Removed)> wrote:

> My question is,
> 1) Why can't lists be hashed?

They are mutable.

Dustan
Guest
Posts: n/a

 09-19-2007
On Sep 18, 7:39 pm, sapsi <(E-Mail Removed)> wrote:
> Hello,
> I recently tried using the set function in Python and was surprised to
> find that
>
> a=[ 1, 2,3, [1,2] ]
>
> doesn't work with 'set', throwing TyperError (unhashable exception). I
> found out that this is because lists can't be hashed.
>
> So,this implies 'a' cannot be a set in python which i think is quite
> unfortunate, after all 'a' does look like a mathematical set.

It is not the variable *a* itself that's a problem when constructing a
set (ie. set(a)); it is the content. set() goes through each of the
items and adds that item to the set. 1, 2, and 3 are valid because
they can be hashed. The next item in the list, however, is [1,2], and
cannot be hashed because it is a mutable list.

The solution is as Raymond Hettinger said:

a = set([1, 2, 3, frozenset([1, 2])])

> My question is,
> 1) Why can't lists be hashed?

They're mutable.

> and
> 2) This is not related, but is there i neat way (without pop and list
> comprehension) to convert a set into a list? I say neat because i'm
> guessing using list comprehension might turn out be slow and there
> might be other methods which are faster.

list(a_set)

> Thank you for your time

You're welcome.

Guest
Posts: n/a

 09-19-2007
On Sep 19, 1:59 am, Raymond Hettinger <(E-Mail Removed)> wrote:
> On Sep 18, 5:39 pm, sapsi <(E-Mail Removed)> wrote:
>
> > I recently tried using the set function in Python and was surprised to
> > find that

>
> > a=[ 1, 2,3, [1,2] ]

>
> > doesn't work with 'set', throwing TyperError (unhashable exception). I
> > found out that this is because lists can't be hashed.
> > So,this implies 'a' cannot be a set in python which i think is quite
> > unfortunate, after all 'a' does look like a mathematical set.

>
> This is written as:
>
> a = set([1, 2, 3, frozenset([1, 2])])
>
> > This is not related, but is there i neat way (without pop and list
> > comprehension) to convert a set into a list?

>
> list(a)
>
> Raymond

frozenset over turning the embedded list into a tuple?
The tuple would preserve order in the item (1,2)
a = set([1,2,3, (1,2)])

Francesco Guerrieri
Guest
Posts: n/a

 09-19-2007
On 9/19/07, Paddy <(E-Mail Removed)> wrote:

> frozenset over turning the embedded list into a tuple?
> The tuple would preserve order in the item (1,2)
> a = set([1,2,3, (1,2)])

The OP was probably thinking in mathematical terms as in "the set of
all the possible subsets of the set composed by 1, 2 and 3" and thus
order would not be important.

francesco

Sion Arrowsmith
Guest
Posts: n/a

 09-19-2007
sapsi <(E-Mail Removed)> wrote:
> Why can't lists be hashed?

Several people have answered "because they're mutable" without
explaining why mutability precludes hashing. So:

Consider a dict (dicts have been in Python a *lot* longer than
sets, and have the same restriction) which allowed lists as
keys:

d = {}
k = [1, 2]
d[k] = None

Now, if I were to do:

k.append(3)

what would you expect:

d.keys()

to return? Did d magically rehash k when it was modified? Did d[k]
take a copy of k, and if so, how deep was the copy (consider
d[[1, k]] = None followed by a modification to k)? Leaving the hash
unchanged and relying on collision detection to resolve won't work,
since you may go directly for d[[1, 2, 3]] and not spot that
there's already an entry for it since it's been hashed under [1, 2].

"Practicality beats purity" and the design decision was to simply
sidestep these issues by disallowing mutable dict keys. And as the
set implementation is based on the dict implementation, it applies
to sets to.

--
\S -- http://www.velocityreviews.com/forums/(E-Mail Removed) -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump

Karthik Gurusamy
Guest
Posts: n/a

 09-19-2007
On Sep 19, 6:16 am, Sion Arrowsmith <(E-Mail Removed)>
wrote:
> sapsi <(E-Mail Removed)> wrote:
> > Why can't lists be hashed?

>
> Several people have answered "because they're mutable" without
> explaining why mutability precludes hashing. So:
>
> Consider a dict (dicts have been in Python a *lot* longer than
> sets, and have the same restriction) which allowed lists as
> keys:
>
> d = {}
> k = [1, 2]
> d[k] = None
>
> Now, if I were to do:
>
> k.append(3)
>
> what would you expect:
>
> d.keys()
>
> to return? Did d magically rehash k when it was modified? Did d[k]
> take a copy of k, and if so, how deep was the copy (consider
> d[[1, k]] = None followed by a modification to k)? Leaving the hash
> unchanged and relying on collision detection to resolve won't work,
> since you may go directly for d[[1, 2, 3]] and not spot that
> there's already an entry for it since it's been hashed under [1, 2].
>
> "Practicality beats purity" and the design decision was to simply
> sidestep these issues by disallowing mutable dict keys. And as the
> set implementation is based on the dict implementation, it applies
> to sets to.

While it's easy to explain the behavior, I think the decision to dis-
allow mutable items as keys is a bit arbitrary. There is no need for
dict to recompute hash (first of all, a user doesn't even need to know
if underneath 'hashing' is used -- the service is just a mapping
between one item to another item).

Since we know hashing is used, all that is needed is, a well-defined
way to construct a hash out of a mutable. "Given a sequence, how to
get a hash" is the problem. If later the given sequence is different,
that's not the dict's problem.

>>> d = {}

a = 10
>>> d[a] = 'foo'
>>> d[5+5] = 'bar'
>>> d[10]

'bar'

aren't the '5+5' which is 10, is different from the previous line's
a?.. so
why not allow similar behavior with lists/other sequence/even other
collections. As long as two objects compare equal the hash-result must
be the same. I guess this takes us to defining the equality operation
for lists-- which I think has a very obvious definition (ie same
length and the ith element of each list compare equal).

So if the list changes, it will result in a different hash and we will
get a hash-miss. I doubt this is in anyway less intuitive than dis-
allowing mutable items as keys.

Karthik

>
> --
> \S -- (E-Mail Removed) --http://www.chaos.org.uk/~sion/
> "Frankly I have no feelings towards penguins one way or the other"
> -- Arthur C. Clarke
> her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump

Vampire
Junior Member
Join Date: Sep 2007
Location: BaT World
Posts: 1

 09-19-2007
Hi,
i am Frm malaysia. i am working in a cafe as network admin and we are having 151 work stations.

Since last 1 week i found that soe work stations keep sending the msg.." windows ip is colflicting from another location: and those pc s can not get online.

but the thing is this Pc keep changing.. not always the same pc is showing this msg:

what could be the reason? and how to ressolve it"??

---VaMpirE----