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" ) |
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" ) |
Initial list of tuple [(11, 22, 33), (44, 55, 66), (11, 23)] Tuples does not have same length
Time complexity: O(n), where n is the number of tuples in the input list. This is because the code uses a single for-loop to iterate through all the tuples and check their length.
Auxiliary space: O(1), as the code only uses a few variables to store intermediate values and no additional data structures are used.
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" ) |
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: O(n), where n is the number of tuples in the input list. The auxiliary space is used to store the length of each tuple in the list.
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 #initialization 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 |
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
Method 5: Using the map() function
Using the map() function to get the length of each tuple and then checking if all the lengths are equal using the set() function.
Python3
Input = [( 11 , 22 , 33 ), ( 44 , 55 , 66 )] # Get the lengths of all tuples using map() lengths = set ( map ( len , Input )) if len (lengths) = = 1 : print ( "All tuples have same length" ) else : print ( "Tuples do not have same length" ) |
All tuples have same length
Time Complexity: O(n), where n is the number of tuples in the input list. This is because the program iterates through each tuple in the list only once to check its length.
Auxiliary Space: O(1), as the program only uses a constant amount of additional space to store the flag variable, regardless of the size of the input list.
Method #6 : Using operator.countOf() method
Approach
- Create a new list with lengths of tuples
- Check whether count of k in lengths list is equal to length of list
- If yes display “All tuples have same length“
- If no display “Tuples does not have same length“
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 import operator if operator.countOf(x,k) = = len ( Input ): print ( "All tuples have same length" ) else : print ( "Tuples does not have same length" ) |
Initial list of tuple [(11, 22, 33), (44, 55, 66)] All tuples have same length
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #7: Using a loop to check length equality
- Iterate through the input tuples and check if their lengths are equal or not.
- If all lengths are equal, then all tuples have the same length.
Python3
def check_tuple_lengths(input_list): n = len (input_list) length = len (input_list[ 0 ]) for i in range ( 1 , n): if len (input_list[i]) ! = length: return "Tuples do not have same length" return "All tuples have same length" input_list = [( 11 , 22 , 33 ), ( 44 , 55 , 66 )] print (check_tuple_lengths(input_list)) |
All tuples have same length
Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(1), no extra data structure is used.