For example, I have a frozen set
[frozenset({'a', 'c,'}), frozenset({'h,', 'a,'})]
I want to convert it to a normal list like
[['a', 'c,'],['a,', 'd,']...]
What method should I use?
Typecasting to list can be done by simply using list(set_name) . Using sorted() function will convert the set into list in a defined order.
Python frozenset() It is immutable and it is hashable. It is also called an immutable set. Since the elements are fixed, unlike sets you can't add or remove elements from the set. Frozensets are hashable, you can use the elements as a dictionary key or as an element from another set.
Frozenset is similar to set in Python, except that frozensets are immutable, which implies that once generated, elements from the frozenset cannot be added or removed. This function accepts any iterable object as input and transforms it into an immutable object.
You can use python list() function to convert set to list.It is simplest way to convert set to list.
sets=[frozenset({'a', 'c,'}), frozenset({'h,', 'a,'})]
print([list(x) for x in sets])
The list comprehension will convert every frozenset in your list of sets and put them into a new list. That's probably what you want.
You can also you map, map(list, sets)
. Please be aware, that in Python 3, if you want the result of map
as list you need to manually convert it using list
, otherwise, it's just a map object
which looks like <map object 0xblahblah>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With