Set Frozenset and Dict Data Type

The set frozenset and dict data type in Python are some of the newer concepts that are different than other languages. These set frozenset and dict data type are the few data structures present in python. The set frozenset and dict data type are of collection type and can come handy while working with various types of problems.


set in Python:

Set is similar to lists but it has a few differences like:

  • In the set data type the order of insertion is not preserved and duplicates are not allowed.
  • It is declared using curly braces.

Example:

#set named 'a' is declared
>>> a= {1, 2, 3, 3, 1, 2}
#printing 'a'
>>> a

Output:{1, 2, 3}

In this code, we can see that the duplicate elements have been declared without any error. But the output will contain every unique element only one single time.

  • Heterogeneous objects are allowed.
  • In set data type indexing and slicing are not applicable as the order of insertion is not preserved.
  • It is mutable in nature i.e. we can add or remove objects.

Example:

#we add an element 'Meow' to the set
>>>a.add(‘Meow’)
>>>a

Output:{1, 2, 3, ‘Meow’}


frozenset in Python:

  • It works the same as a set i.e. order of insertion is not preserved and duplicates are not allowed.
  • It is declared using curly braces.
  • But it is immutable in nature.

Example:

>>> se= {1, 2, 3, 4}
#creating a frozen set
>>> fset= frozenset(se)
>>> type(fset)
<class ‘frozenset’>

Output:frozenset ({4, 2, 3, 1})

In this code above we see that the order of insertion is not maintained just like set data type.


Note : Being of immutable nature we do not have the flexibility to change the objects.
Example:>>> fset.add(5)
Output:AttributeError: ‘frozenset’ object has no attribute ‘add’


dict in Python:

Dict is the abbreviation for the dictionary. Just a real-world dictionary the dict data type contains a key and a corresponding value to that. This means that it contains key-value pairs which have a relation.

  • Duplicate keys are not permitted but the values that are represented by these keys may be duplicate.
  • It is declared using curly braces.
  • Heterogenous values are allowed a key and value can be of any type.
  • It is mutable in nature.

Example:

#we declare the variable 'di'
>>di = {10 : ‘Meow’ , 20 : ‘Woof’ , 30 : ‘Quack’}
>>>di

Output:{10 : ‘Meow’ , 20 : ‘Woof’ , 30 : ‘Quack’}

Note: If we just declare empty curly braces then by default the data type will be dict and not set.

Example:

>>>d= {}
#we only declare empty braces and print the type
>>>type(d)

Output:<class ‘dict’>

Now since it is mutable then we can continue to add on elements to make it grow.

Example:

>>> dic = {}
#we add elements using key
>>> dic[10]= ’Kitty’
>>> dic[11]= ’Meow’
>>>  dic[12]= ’Cat’
#printing 'dic'
>>> dic

Output:{10 : ‘Kitty’ , 11 : ‘Meow’ , 12 : ‘Cat’}

If the key value is repeated then the old value of the key will simply be replaced by the new value.

These was all about collection type data types in Python i.e. set frozenset and dict data type.

Leave a Comment

Your email address will not be published. Required fields are marked *