Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Find whether all tuple have same length

Improve Article
Save Article
  • Last Updated : 01 Feb, 2023
Improve Article
Save Article

Given a list of tuples, the task is to find whether all tuple have same length. 
Below are some ways to achieve the above task.
Method #1: Using Iteration
 

Python3




# Python code to find whether all
# tuple have equal length
 
# Input List initialization
Input = [(11, 22, 33), (44, 55, 66)]
 
# printing
print("Initial list of tuple", Input)
 
# K Initialization
k = 3
flag = 1
 
# Iteration
for tuple in Input:
    if len(tuple) != k:
        flag = 0
        break
 
# Checking whether all tuple
# have length equal to 'K' in list of tuple
if flag:
    print("All tuples have same length")
else:
    print("Tuples does not have same length")

Output: 

Initial list of tuple [(11, 22, 33), (44, 55, 66)]
All tuples have same length

 

Time Complexity: O(n), where n is the number of tuples in the input list.
Auxiliary Space Complexity: O(1), as the space required is constant regardless of the size of the input list.

  
Method #2: Using all()
 

Python3




# Python code to find whether all tuple
# have equal length
 
# Input list initialization
Input = [(11, 22, 33), (44, 55, 66), (11, 23)]
k = 2
 
# Printing
print("Initial list of tuple", Input)
 
# Using all()
Output =(all(len(elem) == k for elem in Input))
 
# Checking whether all tuple
# have equal length
if Output:
    print("All tuples have same length")
else:
    print("Tuples does not have same length")

Output: 

Initial list of tuple [(11, 22, 33), (44, 55, 66), (11, 23)]
Tuples does not have same length

 

Method #3 : Using count() and len() methods

Python3




# Python code to find whether all
# tuple have equal length
 
# Input List initialization
Input = [(11, 22, 33), (44, 55, 66)]
 
# printing
print("Initial list of tuple", Input)
 
# K Initialization
k = 3
x=[]
# Iteration
for tuple in Input:
    x.append(len(tuple))
 
 
# Checking whether all tuple
# have length equal to 'K' in list of tuple
if x.count(k)==len(Input):
    print("All tuples have same length")
else:
    print("Tuples does not have same length")

Output

Initial list of tuple [(11, 22, 33), (44, 55, 66)]
All tuples have same length

Method #4 : Using set to store the lengths of the tuples 

This method uses a set to store the lengths of the tuples in the input list. It then checks if the length of the set is equal to 1, which means that all the tuples have the same length. This method is efficient because it only requires a single pass through the input list and the set data structure allows for fast membership testing.

Python3




def check_tuple_length(input_list):
  # Create a set of the lengths of the tuples in the input list
  lengths = {len(tup) for tup in input_list}
  # Check if the length of the set is equal to 1, which means all the tuples have the same length
  return len(lengths) == 1
#initializaion
Input = [(11, 22, 33), (44, 55, 66)]
Output = check_tuple_length(Input)
print(f"Input: {Input}")
print(f"Output: {Output}")
 
Input = [(11, 22, 33), (44, 55, 66), (11, 23)]
Output = check_tuple_length(Input)
print(f"Input: {Input}")
print(f"Output: {Output}")
#This code is contributed by Edula Vinay Kumar Reddy

Output

Input: [(11, 22, 33), (44, 55, 66)]
Output: True
Input: [(11, 22, 33), (44, 55, 66), (11, 23)]
Output: False

Time complexity: O(n) where n is the number of tuples in the input list
Auxiliary Space: O(n) to store the set of lengths


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!