Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find whether all tuple have same length in Python
In this article, we will learn different methods to check if all tuples in a given list have the same length. This is useful when validating data structures or ensuring consistency in tuple collections.
Using Manual Iteration with len()
We can iterate through each tuple and compare its length to a reference value. If any tuple has a different length, we know they are not all the same ?
schedule = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')]
# Print the original list
print("Given list of tuples:")
print(schedule)
# Check if all tuples have length 3
expected_length = 3
all_same = True
# Iterate through each tuple
for tup in schedule:
if len(tup) != expected_length:
all_same = False
break
# Display result
if all_same:
print("Each tuple has same length")
else:
print("All tuples are not of same length")
Given list of tuples:
[('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')]
Each tuple has same length
Using all() with len()
A more Pythonic approach uses the all() function with a generator expression to check all tuple lengths in one line ?
schedule = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')]
# Print the original list
print("Given list of tuples:")
print(schedule)
# Check if all tuples have length 3
expected_length = 3
all_same = all(len(tup) == expected_length for tup in schedule)
# Display result
if all_same:
print("Each tuple has same length")
else:
print("All tuples are not of same length")
Given list of tuples:
[('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')]
Each tuple has same length
Dynamic Length Checking
Instead of hardcoding the expected length, we can use the first tuple's length as reference ?
# Example with different lengths
mixed_data = [('A', 'B'), ('X', 'Y', 'Z'), ('P', 'Q')]
print("Given list of tuples:")
print(mixed_data)
# Use first tuple's length as reference
if mixed_data:
reference_length = len(mixed_data[0])
all_same = all(len(tup) == reference_length for tup in mixed_data)
if all_same:
print(f"All tuples have same length: {reference_length}")
else:
print("Tuples have different lengths")
else:
print("Empty list")
Given list of tuples:
[('A', 'B'), ('X', 'Y', 'Z'), ('P', 'Q')]
Tuples have different lengths
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Manual Iteration | Good | Fast (early exit) | Learning, debugging |
| all() with Generator | Excellent | Fast (lazy evaluation) | Production code |
| Dynamic Reference | Excellent | Fast | Unknown expected length |
Conclusion
Use all() with a generator expression for clean, readable code. The manual iteration approach offers more control for complex validation logic. Both methods efficiently handle early termination when a mismatch is found.
