Python : Tuple Comprehensions - Exercises and Solution


1. Squares of numbers from 1 to 10 as tuples

Sample Output

(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)

View Solution

2. Even numbers from 1 to 20 as tuples

Sample Output

(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

View Solution

3. Tuple of characters in a string

Sample Output

hello

('h', 'e', 'l', 'l', 'o')

View Solution

4. Length of words in a sentence as tuples

Sample Output

This is a sample sentence

(4, 2, 1, 6, 8)

View Solution

5. Vowels in a sentence as tuples

Sample Output

Hello, how are you?

('e', 'o', 'o', 'a', 'e', 'o', 'u')

View Solution

6. Tuple of distinct prime factors of numbers in a list

Sample Output

[10, 15, 20, 25]

(2, 5, 3, 5, 2, 5, 5)

View Solution

7. Tuple of distinct characters in a list of strings

Sample Output

['apple', 'banana', 'cherry']

('a', 'p', 'p', 'l', 'e', 'b', 'a', 'n', 'a', 'n', 'a', 'c', 'h', 'e', 'r', 'r', 'y')

View Solution

8. Tuple of ASCII values for characters in a string

Sample Output

hello

(104, 101, 108, 108, 111)

View Solution

9. Tuple of common letters between two words

Sample Output

apple

banana

('a',)

View Solution

10. Tuple of even squares up to 100

Sample Output

(0, 4, 16, 36, 64, 100)

View Solution

11. Tuple of positive numbers from a list

Sample Output

[-5, 10, -15, 20, -25]

(10, 20)

View Solution

12. Tuple of distinct consonants in a sentence

Sample Output

Hello, how are you?

('h', 'l', 'l', 'h', 'w', 'r', 'y')

View Solution

13. Tuple of common elements between two lists

Sample Output

[1, 2, 3, 4, 5]

[4, 5, 6, 7, 8]

(4, 5)

View Solution

14. Tuple of distinct vowels in a list of words

Sample Output

['apple', 'banana', 'cherry', 'date']

('a', 'e', 'a', 'a', 'a', 'e', 'a', 'e')

View Solution

15. Tuple of distinct words starting with vowels in a sentence

Sample Output

Hello, how are you?

('are',)

View Solution

16. Tuple of sums of digits in numbers

Sample Output

[123, 456, 789]

(6, 15, 24)

View Solution

17. Tuple of words with length greater than 3 in a sentence

Sample Output

This is a sample sentence

('This', 'sample', 'sentence')

View Solution

18. Tuple of pairs of elements and their squares

Sample Output

[1, 2, 3, 4, 5]

((1, 1), (2, 4), (3, 9), (4, 16), (5, 25))

View Solution

19. Tuple of Negative numbers from a list

Sample Output

[-5, 10, -15, 20, -25]

(-5, -15, -25)

View Solution

20. Tuple of positive and negative numbers from a list

Sample Output

[10, -5, 20, -15, 30]

(10, 20, 30)

(-5, -15)

View Solution

21. Tuple of pairs of words and their lengths

Sample Output

['apple', 'banana', 'cherry', 'date']

(('apple', 5), ('banana', 6), ('cherry', 6), ('date', 4))

View Solution

22. Tuple of characters and their corresponding ASCII values

Sample Output

['a', 'b', 'c', 'd', 'e']

(('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101))

View Solution

23. Tuple of pairs of numbers and their sum from two lists

Sample Output

[1, 2, 3]

[4, 5, 6]

((1, 4, 5), (1, 5, 6), (1, 6, 7), (2, 4, 6), (2, 5, 7), (2, 6, 8), (3, 4, 7), (3, 5, 8), (3, 6, 9))

View Solution

24. Tuple of odd numbers from 1 to 20

Sample Output

(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)

View Solution

25. Tuple of prime numbers up to 50

Sample Output

(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)

View Solution

26. Tuple of factors of numbers in a list

Sample Output

[10, 15, 20, 25]

((10, [1, 2, 5, 10]), (15, [1, 3, 5, 15]), (20, [1, 2, 4, 5, 10, 20]), (25, [1, 5, 25]))

View Solution

27. Tuple of distinct vowels and consonants from a list of words

Sample Output

['apple', 'banana', 'cherry', 'date']

('a', 'e', 'a', 'a', 'a', 'e', 'a', 'e')

('p', 'p', 'l', 'b', 'n', 'n', 'c', 'h', 'r', 'r', 'y', 'd', 't')

View Solution

28. Tuple of tuples with word and its vowel count in a sentence

Sample Output

Hello, how are you?

(('Hello,', 2), ('how', 1), ('are', 2), ('you?', 2))

View Solution

29. Tuple of distinct divisors of numbers in a list

Sample Output

[10, 15, 20, 25]

({1, 2, 10, 5}, {1, 3, 5, 15}, {1, 2, 4, 5, 10, 20}, {1, 5, 25})

View Solution

30. Tuple of words with at least one vowel in a sentence

Sample Output

Hello, how are you?

('Hello,', 'how', 'are', 'you?')

View Solution

31. Tuple of common letters between words of different lengths

Sample Output

apple

cherry

('e',)

View Solution

32. Tuple of tuples with word and its reversed form

Sample Output

['apple', 'banana', 'cherry', 'date']

(('apple', 'elppa'), ('banana', 'ananab'), ('cherry', 'yrrehc'), ('date', 'etad')))

View Solution

33. Tuple of distinct substrings from a word

Sample Output

hello

('h', 'he', 'hel', 'hell', 'hello', 'e', 'el', 'ell', 'ello', 'l', 'll', 'llo', 'l', 'lo', 'o')

View Solution

34. Tuple of tuples with element and its factorial

Sample Output

[1, 2, 3, 4, 5]

((1, 1), (2, 2), (3, 6), (4, 24), (5, 120))

View Solution

35. Tuple of pairs of words and their common letters

Sample Output

apple

cherry

(('e', 'e'),)

View Solution

36. Tuple of words containing 'a' or 'e' in a sentence

Sample Output

This is a sample sentence with various words.

('a', 'sample', 'sentence', 'various')

View Solution

37. Tuple of pairs of numbers and their product from two lists

Sample Output

[1, 2, 3]

[4, 5, 6]

((1, 4, 4), (1, 5, 5), (1, 6, 6), (2, 4, 8), (2, 5, 10), (2, 6, 12), (3, 4, 12), (3, 5, 15), (3, 6, 18))

View Solution

38. Tuple of distinct words with length greater than 4 in a sentence

Sample Output

This is a sample sentence with words of various lengths.

('sample', 'sentence', 'words', 'various', 'lengths.')

View Solution

39. Unique elements from a list as a tuple

Sample Output

[1, 2, 2, 3, 4, 4, 5, 5]

(1, 2, 3, 4, 5)

View Solution

40. Unique characters in a string as a tuple

Sample Output

hello

('o', 'e', 'l', 'h')

View Solution

41. Unique words in a sentence as a tuple

Sample Output

This is a sample sentence with repeated words is

('This', 'words', 'sample', 'with', 'a', 'repeated', 'sentence', 'is')

View Solution

42. Distinct elements from multiple lists while preserving order as a tuple

Sample Output

[1, 2, 3, 4, 5]

[4, 5, 6, 7, 8]

(1, 2, 3, 4, 5, 6, 7, 8)

View Solution

43. Pairs of distinct words and their reversed forms in a sentence as a tuple

Sample Output

Hello, how are you?

(('are', 'era'), ('you?', '?uoy'), ('Hello,', ',olleH'), ('how', 'woh'))

View Solution

44. Distinct elements from a list of mixed data types as a tuple

Sample Output

[1, 'apple', 2.5, 'banana', 3, 'cherry']

(1, 2.5, 3, 'cherry', 'apple', 'banana')

View Solution

45. Pairs of distinct elements and their sum of digits, using divmod(), from two lists as a tuple

Sample Output

[123, 456, 789]

[234, 567, 890]

((123, 234, 42), (456, 890, 140), (789, 890, 176), (456, 567, 114), (456, 234, 78), (123, 890, 104), (123, 567, 78), (789, 234, 114), (789, 567, 150))

View Solution

46. Distinct elements from multiple lists using set symmetric difference, as a tuple

Sample Output

[1, 2, 3, 4]

[3, 4, 5, 6]

[5, 6, 7, 8]

(1, 2, 7, 8)

View Solution

47. Pairs of numbers and their sum, with even and odd pairs separated, from two lists as a tuple

Sample Output

[1, 2, 3]

[4, 5, 6]

(((1, 5, 6), (2, 4, 6), (2, 6, 8), (3, 5, 8)), ((1, 4, 5), (1, 6, 7), (2, 5, 7), (3, 4, 7), (3, 6, 9)))

View Solution

48. Pairs of distinct elements and their sum of digits from two lists as a tuple

Sample Output

[123, 456, 789]

[234, 567, 890]

((123, 234, 15), (123, 567, 24), (123, 890, 23), (456, 234, 24), (456, 567, 33), (456, 890, 32), (789, 234, 33), (789, 567, 42), (789, 890, 41))

View Solution

49. Distinct characters from multiple strings with case-insensitivity as a tuple

Sample Output

['apple', 'Banana', 'Cherry']

('e', 'h', 'b', 'n', 'c', 'r', 'p', 'y', 'a', 'l')

View Solution

50. Pairs of distinct words and their lengths, excluding words with lengths not divisible by 3, in a sentence as a tuple

Sample Output

Hello, how are you?

(('how', 3), ('Hello,', 6), ('are', 3))

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial