Python Data Structures – Tuples & Sets

Python Tuples & Sets - Heterogeneous -Immutability

Tuples:

Tuple is a collection of values.

🍁 It is Heterogeneous. i.e., It can accept any data type of values in a single tuple object.

🍁 Tuple is Completely Immutable.

🍁 Immutable Elements: Cannot change data once declared.

🍁 Immutable Object: A tuple cannot be extended and cannot be cut.

However, elements can be retrieved using index similar to Lists.

Accessing Elements:

tuple1 = (1, 3, 'test', 2, 23)
print("Type:", type(tuple1))
print("Elements till 3rd index:", tuple1[:3])
Type: <class 'tuple'>
Elements till 3rd index: (1, 3, 'test')

It can be noticed that the elements are enclosed with brackets ( ) whereas the List is enclosed with square brackets [ ].

Negative Indexing – Reverse:

Tuples allow negative indexing to step in reverse order.

tuple1[::-1]
(23, 10, 2, 3, 1)

Immutable Elements:

Tuples does not allow item assignment. It will throw “TypeError: ‘tuple’ object does not support item assignment”.

tuple1 = (1, 3, 45, 10, 23)
print(tuple1[2])
tuple1[2] = 45
45

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-5667996273af> in <module>
      1 tuple1 = (1, 3, 45, 10, 23)
      2 print(tuple1[2])
----> 3 tuple1[2] = 45

TypeError: 'tuple' object does not support item assignment

Immutable Object:

Tuple only provides 2 functions. They are count and index. It does not provide functions to append or extend.

Tuple
Functions in Tuple

Sets:

Set is also a collection of elements and it is Heterogeneous. i.e., It can accept any data type of values in a single tuple object.

Immutable Elements: Elements of the set cannot be changed. Item assignment is not allowed in sets.

Immutable Object: Set can be grown or can be cut.

Does not allow Duplicates – Even if a list is converted to set, the duplicates will be removed.

Unordered:

When it is said unordered, it means, an item added as last item is not expected to be seen at end of the set when it is printed.

To understand more clear, Sets can be compared with Lists.

When an item is added to a list its like placing a tablet in a pill box. It is known that monday’s tablet must be in the place where Monday is written. This is how list works.

It is like putting a tablet in a container/bottle and you cannot predict at what place/position exactly the tablet is.

List Vs Set
List Vs Sets

As it is not ordered, there is no index in a set. So, it’s elements cannot be accessed using an index.

Defining a Set:

A set is defined as elements enclosed in curly braces.

set1 = {25,5,4,10,54,'a','test'}
print(set1)
{4, 5, 10, 'a', 54, 25, 'test'}

Accessing Item using index is not possible in Sets:

set1[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-c38563f1af7a> in <module>
----> 1 set1[0]

TypeError: 'set' object is not subscriptable

Pop function:

Pop function does not take any argument. It will just remove and return a element in random choice.

print("Before Pop:", set1)
set1.pop()
print("After Pop:", set1)
Before Pop: {4, 5, 10, 'test', 'a', 54, 25}
After Pop: {5, 10, 'test', 'a', 54, 25}

Remove function:

Remove function takes exactly one argument which must be a member of the Set object.

set1 = {25, 5, 4, 10, 54, 'a', 'test'}
print("Before remove:", set1)
set1.remove('test')
print("After remove:", set1)
Before remove: {4, 5, 10, 'test', 'a', 54, 25}
After remove: {4, 5, 10, 'a', 54, 25}

If the given value does not exists in the set, then the “KeyError” is raised.

set1.remove("random element")
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-31-c81cabf8adba> in <module>
----> 1 set1.remove("random element")

KeyError: 'random element'

Asha Ponraj
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 86

Leave a Reply

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