Sets in Python

Paddy paddy3118 at googlemail.com
Wed Sep 19 03:00:00 EDT 2007


On Sep 19, 1:59 am, Raymond Hettinger <pyt... at rcn.com> wrote:
> 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

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)])

- Paddy.




More information about the Python-list mailing list