+ 2
Sets(& frozen sets)
So I was reading info on sets and found that sets are mutable and they are like the only ''key'party of a dictionary..But I also read that sets can contain only immutable objects (strings, numbers and tuples) because it's just like a key which cannot be changed in a dictionary (correct me if I'm wrong).. I don't understand.. in what ways are sets mutable then??(is it through their set operations??) 2.I also read that frozen sets are the immutable version of set but couldn't find info on it..Any details on this will be gladly appreciated...
7 Respostas
+ 2
A set is mutable because you can add or remove values from it (change it's internal state). The values themselves however, can not be changed (you can't change a tuple), and thus are immutable.
The comparison with dictionary keys can be made because each key in a dictionary is unique, and so is each value of a set. Therefore, the values of a set can be the keys of dictionary.
+ 2
Let me compare a set with a tuple:
{1, 2, 3, 'g', (5, 6)}
(4, 5, ['a', 'b'])
So a set is mutable: You can for example add more elements, or delete some, let's say the 'g' or the tuple (5,6). But whatever you put into or take from the set, may not be mutable! So for example you can not put lists into a set.
The tuple acts the other way round: You can not add, delete or move around things in the tuple. But the elements themselves can be mutable, so you can put lists into a tuple. You can even change the list's content, erase the 'a', append a 'c'; but you can not delete or move the list itself, because it's part of the tuple.
+ 2
a frozen set is a set that can't be changed anymore.
(I've never used it for anything so far.)
+ 2
You use frozensets when you want to make sets of sets (being hashed set elements need to be nkn mutable, which is the case for frozensets) or use sets as dictionnary keys.
You do this by creating a set or a list then "freezing" it with fronmzenst()
+ 1
thnx everyone!!
+ 1
And what is a frozen set exactly??and how do you use it?? Any info will be valued..
+ 1
thnx everyone!