Use case for Python Tuple

Most Python programmers are guilty (me included) of always using List without even considering Tuple at all. Surely for some use cases, Tuple should be better, right? Keep reading for one such case.


# Create Tuple with two elements
tup = (1, [2])
# Re-assign first element
tup[0] = 3
Tuple is immutable so it doesn’t support item assignment.
# Add element to existing List element inside Tuple
If Tuple element is list then it can be altered.

Use Case

Option 1: Using List

Python Programming: endswith method will not accept List of string so we need to call this method for every item in the list. This could be computational ly expensive when there are lot of elements in the list.
Find all the image files using List

Option 2: Using Tuple

Python Programming: endswith method will accept Tuple of string so we just have to call it once. The code is cleaner and easier to read as well. This way of scripting is better because we are utilising the immutability functionality of the Tuple.
Find all the image files using Tuple


Sharing my journey with Python, Computer Vision, Deep Learning and everything related to it. This includes the good, the bad and the ugly.