# What is Frozenset in Python

## set frozenset

### introduction

The sets in python (set objects) were modeled on those used in mathematics.

But what is special about quantities?

Sets are DISORDERED collections of objects that appear only once in the set object.

So you cannot rely on an index to point to a certain value at a certain position in the network (as with *st*, *list*, *tuple*), you still have one *key*, like one *dict*ionary.

And if you pass an ordered list (set (ordered_list)), there is no guarantee that the order will be maintained. The (operating) system decides how and where the data should be located in the RAM, so this can also provide different outputs from system to system. So the order is at *disordered* Data types are never a criterion.

There are two types of objects in python:

objects of type set are mutable, which is not the case with frozenset objects.Sets can be converted into a frozenset in no time and vice versa.

Which set operations are there?

- x is an element of the set S
- x is not an element of S
- S1 is equal to S2
- S1 is not equal to S2
- S1 is *
**real*** Subset of S2 - S1 is *
**fake*** Subset of S2 - S1 is *
**real*** superset from S2 - S1 is *
**fake*** superset from S2 - Average of S1 and S2
- S1 unites with S2
- Difference between S2 and S1
- Symmetrical difference between S2 and S1

These set operations can also be performed on **d.keys ()**- and **d.items ()**Objects (d is a dict object) can be applied!

### set

Immediately in-medias-res

>>> x = (1,1,2,2,3,3,4,5,6) >>> y = set (x) >>> print (y) {1, 2, 3, 4, 5 , 6} >>> print (type (y))this is how you build a set; more samples

>>> a = {1, 2, 3, 2, 3} >>> print (a) {1, 2, 3} >>> type (a) We could also use the functions *set()* and *frozenset ()*to create empty set objects because it still holds

results in an empty dictionary object!

A set may / can only contain unchangeable objects!

So...

- not a list object
- no dict object

In addition, nesting of sets is not possible!

>>> quantity = {2, [1,2], 'a'} Traceback (most recent call last): File "Let's look at the following prime example, where a check is made to see how often an item occurs in a sequence.

>>> wort = 'Donaudampfschifffahrtsgesellschaft' >>> for c in sorted (list (set (word))): ... print ('{}: {} x'.format (c, wort.count (c)) ) ... a: 4x c: 2x d: 2x e: 2x f: 5x g: 1x h: 3x i: 1x l: 2x m: 1x n: 1x o: 1x p: 1x r: 1x s: 4x t : 2x u: 1x >>>It continues with various operations.

>>> m1 = set (range (10)) >>> m2 = set (range (5,15)) >>> print ('m1 combined with m2 = {}'. format (m1 | m2)) m1 combined with m2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} >>> print ('m1 combined with m2 = {}'. format (m1.union (m2))) m1 united with m2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} >>> print ( 'Average of m1 and m2 = {}'. Format (m1 & m2)) Average of m1 and m2 = {8, 9, 5, 6, 7} >>> print ('Average of m1 and m2 = {}' .format (m1.intersection (m2))) Average of m1 and m2 = {8, 9, 5, 6, 7} >>> print ('The elements {} of m1 are not in m2'.format (m1 - m2)) The elements {0, 1, 2, 3, 4} of m1 are not in m2 >>> print ('The elements {} of m2 are not in m1'.format (m2.difference (m1))) The elements {10, 11, 12, 13, 14} of m2 are not in m1 >>> That reminds me a lot of the set theory of the first grade in grammar school. **|** so is the same *union*() and *&* Average (s1.intersection (s2).

Let's take a look at other set methods using the following examples.

clear that this is not possible - the set object is a DISORDERED type -> no index query (m4 [0]) makes sense (who knows / cares what is on position 0!)

>>> m5 = m3 >>> print ('id-m3: {0} \ nid-m4: {1} \ nid-m5: {2}'. format (id (m3), id (m4), id (m5))) id-m3: 3083592508 id-m4: 3083594300 id-m5: 3083592508 >>> The *copy*() Method of set actually has its own set object (its own id) built.

Using the *discard*() Method we can remove an item from a set (if the object specification that is to be removed from the set object is not included in the set object, there is no noise / error / exception) - we will afterwards nor the *remove*() Method and the *pop*() Method that returns an object from the set object **to catch** pops out of the set.

It seems that pop () pops out the first item from the set object, but since there is no **Positions** there, it is impossible to from **position** to speak.

#### On the subject vs (subset)

>>> quantity1 = {0,1,2,3} >>> quantity2 = {1,2} >>> quantity3 = set (range (4)) >>> quantity2 <= quantity1 True >>> Quantity2 is a subset of Quantity1.

is it a real or a spurious subset of set1?

Quantity2 **IS REAL** Subset of set 1

Set3 is also a subset of Set1, but a **FAKE**.

#### symmetric_difference ()

m1.symmetric_difference (m2) can also be written like this:

and that does it:

unites difference m1-m2 with m2-m1

(m1 - m2) | (m2 - m1)

so

and that was achieved in the following way

the built-in functions *len*(), *Max*() or *min*() (and probably others) also work for (frozen) set objects!

Sometimes you wonder why there are the set methods on the one hand and the characters like on the other

gives?

with m1 & m2 m1, m2 MUST be set / frozenset objects, while with set / frozenset methods the transferred objects MUST be only. **iterable** need to be

SETOBJECT.symmetric_difference (STRING)

Example:

### set comprehension

We already know *list comprehension* and *dict comprehension*.

And so set comprehensions are also a convenient way of creating a set object.

The similarity to dict-comprehensions is great, so be careful!

You can see again how messy the items are in the set object.

### frozenset

frozenset objects can *NOT* be changed (immuteable).

Frozensets objects can therefore also be used as a key in a dictionary or be an element of a set object.

Any methods that did not change the set object also apply to frozensets.

Frozen sets are created as follows

What if I frozenset with set.

>>> wasbinich = m1.union (m7) >>> print (type (wasbinich))So a set object comes out.

>>> s_name = set (m7) >>> print (type (s_name))so you can effortlessly turn a set into a frozenset object and vice versa.

Here it goes to the top of the page and there it goes to the python start page

- What do guys think of stretch marks
- Where does the word later come from?
- How is vtu ended
- What is the conjugate base of H2PO4
- People really buy downvotes on Reddit
- What is the best sponsorship email database
- Munich is a centered city in Europe
- What are some fun learning apps
- Why isn't renewable energy so cheap?

- Can we find another habitable planet
- What is Cyclone Titli
- What does Join do in PHP
- Are digital clothing the future
- What does This Is London Baby mean
- EXO Lay withdraws from the group
- What material is used in skater tubes
- What is a child's psychological rating
- What causes herpes on my thighs
- Art theory is a different art
- Is there any scientific evidence for women's intuition
- Is mythology a story of real events
- Can the imbalance of the inner ear be corrected
- What is a Promethean coffin
- Which is the best smart remote app
- How did Emily Dickinson die
- Can someone lose their trust
- What makes Jack the Ripper so famous
- What is Google used for on purpose?
- Why is superfinishing necessary
- Can a cork go in the oven
- Most prisoners in prison have thighs
- Where was Charles Manson born and raised there?
- Who promotes tourism in Kenya