Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert frozenset to normal sets or list?

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?

like image 238
Alex Z Avatar asked Oct 08 '16 08:10

Alex Z


People also ask

How do you change a set into a list?

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.

Why is a Frozenset () different from a regular set?

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.

What is the difference between a set and a Frozenset?

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.

Can we convert set to list in Python?

You can use python list() function to convert set to list.It is simplest way to convert set to list.


1 Answers

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>

like image 193
Ivan Avatar answered Oct 10 '22 19:10

Ivan