পদ্ধতি ১: for loop + len()
listA = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')]
k = 3
res = 1
for tup in listA:
if len(tup) != k:
res = 0
break
if res:
print("Each tuple has same length")
else:
print("All tuples are not of same length")
পদ্ধতি ২: all() + len()
listA = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')]
k = 3
res = all(len(tup) == k for tup in listA)
if res:
print("Each tuple has same length")
else:
print("All tuples are not of same length")
ব্যাখ্যা
all() শর্ট-সার্কিট করে — প্রথম ফলস পেলে থামে।
- জেনারেটর এক্সপ্রেশন মেমরি সেভ করে।
- পদ্ধতি ২ বেশি পাইথনিক এবং কনসাইজ।
আউটপুট
Each tuple has same length