LearnFrenzy
Sign in Take a free test

Tuples · Tuples Coding Problems · Question 8

Find tuples whose elements are all divisible by k in Python Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

From a list of tuples, keep only those whose every element is divisible by k = 3.

Solution
main.py Run
data = [(6, 9, 12), (3, 4), (15, 30), (), (7,)]
k = 3
result = [t for t in data if t and all(x % k == 0 for x in t)]
print(result)

Output Verified by running it on Python 3

[(6, 9, 12), (15, 30)]

all() is True for an empty tuple, so "t and" excludes () explicitly.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links