Sometimes I have a list and I want to do some things with it. I write things like:
>>> mylist = [1,2,3]
>>> myset = set(mylist)
{1, 2, 3}
Today I discovered that from Python 2.7 you can also define a collection directly {1,2,3}, and it seems like an equivalent way to define it.
Then I wondered if I could use this syntax to create a set from this list.
{list}fails because it tries to create a collection with only one element - a list. And the lists resolve.
>>> mylist = [1,2,3]
>>> {mylist}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
So, interestingly: is there a way to create a collection from a list using the syntax {}instead set()?
source
share