Helpful Python tuple operations
Data structures are the fundamentals of any programming language. A tuple is one of the four built-in data type structures in Python. This article is in continuation of our previous article on the similarities and differences between a Python tuple and a Python list. This article list some of the helpful and commonly used Python tuple operations.
A tuple is a collection of Python objects in a sequence. Many times while programming it becomes essential to check the presence of an element in a collection.
There are several methods to perform this task and these methods return boolean values true or false.
Using ‘in’ to verify the existence of an object in a tuple
This is a very straightforward operation that can be easily used in most cases.
word = 'B','A','L','L','O','O','N'
print('A' in word)
#OUTPUT True
Iterating a mixed tuple collection to check the presence
Not all tuple objects will be as simple as the above example, where the tuple is a collection of string-type objects. Now, making the above case a little more challenging by making it a mixed collection would require a different approach such as iterating the collection in a loop. This method is also known as the brute force method.
word = ['B','A','L','L'],'O','O','N'
print('A' in word)
#OUTPUT False
Here the steps of the algorithm will be:
- Assign a variable to match
- Start a loop
- Check if the selected object type is a list
- Match the assigned variable in the list
- When; True
- Break loop and exit
- When; False
- Match every character against the assigned variable
- When; True
- Break loop and exit
- Print False
word = ['B','A','L','L'],'O','O','N'
char = 'A'
for chars in word:
if type(chars) is list:
print(char in chars)
break
if chars == char:
print(True)
break
print(False)
#OUTPUT True
Using the list comprehension method to filter an object in a tuple
A list comprehension means iterating through the collection of objects in the list collection in one line instead of multiple lines as shown above. Here the steps of the algorithm will be:
- Assign a variable to match
- Convert the tuple to a list and extract the character matching the variable.
- Check if the character is in the list and print the result.
word = 'B','A','L','L','O','O','N'
char = 'A'
print([i for i in word if i == char])
#OUTPUT ['A']
print([i for i in word if i != char])
#OUTPUT ['B', 'L', 'L', 'O', 'O', 'N']
Python tuple operations related to the length of a tuple
Just like a Python list the length of a tuple can be determined by the len function.
sentence = 'green','colour','balloon'
print (len(sentence))
#OUTPUT 3
Similarly, to find the object with the maximum length and minimum length can be determined by max() & min().
rates = 50,10,20,30
print (min(rates))
#OUTPUT 10
print (max(rates))
#OUTPUT 50
I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.
Discuss