DEV Community

Cover image for Use case for Python Tuple
Aravind Ramalingam
Aravind Ramalingam

Posted on • Updated on • Originally published at 6aravind.Medium

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.

Tuple is like List but immutable so it has lesser in built methods. Tuples are faster than Lists, but it is noticeable only when size of the tuple is considerably large. For a refresher on Tuples, check out this blog

Caveat

A general rule of thumb is to use Tuple only when the data will or should not change.

# Create Tuple with two elements
tup = (1, [2])

# Re-assign first element
tup[0] = 3
Enter fullscreen mode Exit fullscreen mode

Alt Text

This does not mean you really can’t change a tuple. If the underlying tuple element is mutable then we can change it. This could lead to unintended side effects. For example, children/inherited class can modify a tuple element initialised by the parent class.

# Add element to existing List element inside Tuple
tup[1].append(3)

print(tup)
Enter fullscreen mode Exit fullscreen mode

Alt Text

Use Case

Personally, I am more into Deep Learning and have to deal with image files a lot. Image files have many popular formats like jpg, png and svg etc. We often have a situation where we have to find all files of certain types from a folder. There are many ways of going about this. We will explore few options.

Option 1: Using List

The filtering based on the extension type is done with the help of endswith method. Since the endswith method does not accept list we are looping through all the extension elements. The code looks readable and takes about 2 microseconds which is not bad, but for every extension type, we have to run through all the files one more time. I have a suspicion that we can do much better.

# List of files in the folder
all_files = ['cat.jpg', 'dog.png', 'report.docx', 'sales.csv']

# List of image file extensions
img_exts_lst = ['jpg','png']

# Filter only image files using List
img_files = [file for file in all_files for ext in img_exts_lst if file.endswith(ext)]

print(img_files) # ['cat.jpg', 'dog.png']

Enter fullscreen mode Exit fullscreen mode

Option 2: Using Tuple

Almost similar code but we are able to pass the entire Tuple as parameter to the endswith method. This removes the extra loop and makes the code even shorter. The code now runs about 2 times faster. To run your own experiments use this notebook as base.

# List of files in the folder
all_files = ['cat.jpg', 'dog.png', 'report.docx', 'sales.csv']

# Tuple of image file extensions
img_exts_tup = ('jpg','png')

# Filter only image files using Tuple
img_files = [file for file in all_files if file.endswith(img_exts_tup)]

print(img_files) # ['cat.jpg', 'dog.png']
Enter fullscreen mode Exit fullscreen mode

Conclusion

Some methods like startswith and endswith accept Tuple as parameter, if the data does not change then better to use Tuple or convert List to Tuple before passing data to the method. Tuples make the code look more elegant and run faster. Also, don’t forget about the caveat.

Hopefully this post helps you to be more open to tuples. If you have any questions or thoughts, feel free to reach out via comments down here or Twitter.

Top comments (0)