Helpful Python tuple operations

By Abhinash on September 19, 2022

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.

EXAMPLE

Is the character “A” present in the tuple x = 'B','A','L','L','O','O','N'

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:

  1. Assign a variable to match
  2. Start a loop
  3. Check if the selected object type is a list
  4. Match the assigned variable in the list
  5. When; True
    1. Print
    2. Break loop and exit
  6. When; False
    1. Match every character against the assigned variable
    2. When; True
      1. Print
      2. Break loop and exit
  7. 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:

  1. Assign a variable to match
  2. Convert the tuple to a list and extract the character matching the variable.
  3. 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
NOTES

I am currently working as a Research Associate. My work is centered on Macroeconomics with modern econometric approach. Broadly, the methodological research focuses on Panel data and Times series data analysis for causal inference and prediction. I also served as a reviewer to Journals of Taylor & Francis Group, Emerald, Sage.

Discuss