Sets in Python

Raymond Hettinger python at rcn.com
Tue Sep 18 20:59:02 EDT 2007


On Sep 18, 5:39 pm, sapsi <saptarshi.g... at gmail.com> 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




More information about the Python-list mailing list