Python : Set Comprehensions - Exercises and Solution


1. Generate a set of squares of numbers from 1 to 10

Sample Output

{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

View Solution

2. Create a set of even numbers from 1 to 20

Sample Output

{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

View Solution

3. Generate a set of characters from a string

Sample Output

Hello, world!

{'e', 'r', 'o', 'H', 'w', 'l', 'd'}

View Solution

4. Create a set of lengths of words in a sentence

Sample Output

This is a sample sentence.

{1, 2, 4, 6, 9}

View Solution

5. Generate a set of prime numbers from 1 to 50

Sample Output

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

View Solution

6. Create a set of lowercase letters

Sample Output

{'a', 'e', 'u', 'z', 'y', 'j', 'k', 't', 'x', 'd', 'r', 'v', 'o', 'h', 'f', 'i', 'c', 'g', 'l', 'p', 'b', 'n', 'm', 'q', 's', 'w'}

View Solution

7. Generate a set of uppercase letters

Sample Output

{'U', 'M', 'F', 'D', 'A', 'I', 'R', 'Y', 'V', 'N', 'T', 'P', 'X', 'O', 'C', 'L', 'W', 'Q', 'K', 'J', 'H', 'Z', 'E', 'G', 'S', 'B'}

View Solution

8. Create a set of even numbers squared and odd numbers cubed from 1 to 10

Sample Output

{64, 1, 4, 36, 100, 16, 343, 729, 27, 125}

View Solution

9. Generate a set of common multiples of 3 and 5 up to 100

Sample Output

{75, 45, 15, 90, 60, 30}

View Solution

10. Create a set of reversed strings from another set

Sample Output

{'cherry', 'apple', 'banana'}

{'ananab', 'elppa', 'yrrehc'}

View Solution

11. Generate a set of positive square roots from a set of positive numbers

Sample Output

{16, 1, 4, 9, 25}

{1.0, 2.0, 3.0, 4.0, 5.0}

View Solution

12. Create a set of uppercase words from a sentence

Sample Output

This is a sample sentence.

{'SAMPLE', 'THIS', 'SENTENCE.', 'A', 'IS'}

View Solution

13. Generate a set of non-vowel characters from a string

Sample Output

Hello, world!

{' ', '!', 'w', ',', 'r', 'H', 'l', 'd'}

View Solution

14. Create a set of unique numbers from a list

Sample Output

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

{1, 2, 3, 4, 5}

View Solution

15. Generate a set of ASCII values of characters from a string

Sample Output

Hello, world!

{32, 33, 100, 101, 72, 108, 44, 111, 114, 119}

View Solution

16. Generate a set of tuples containing numbers and their squares

Sample Output

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

View Solution

17. Create a set of vowels from a string

Sample Output

Hello, world!

{'e', 'o'}

View Solution

18. Generate a set of numbers that are perfect squares from 1 to 100

Sample Output

{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

View Solution

19. Generate a set of characters that are digits from a string

Sample Output

12345Hello67890

{'2', '8', '7', '3', '9', '0', '1', '5', '4', '6'}

View Solution

20. Create a set of numbers that are powers of 2 from 1 to 10

Sample Output

{32, 64, 2, 128, 4, 256, 512, 1024, 8, 16}

View Solution

21. Generate a set of common elements from two lists

Sample Output

[1, 2, 3, 4, 5]

[3, 4, 5, 6, 7]

{3, 4, 5}

View Solution

22. Generate a set of characters that are not alphanumeric from a string

Sample Output

Hello, world!

{'!', ' ', ','}

View Solution

23. Create a set of characters that are consonants from a string

Sample Output

Hello, world!

{'H', 'r', 'w', 'd', 'l'}

View Solution

24. Create a set of strings with characters in uppercase

Sample Output

{'banana', 'apple', 'cherry'}

{'APPLE', 'BANANA', 'CHERRY'}

View Solution

25. Create a set of words with their characters sorted

Sample Output

{'banana', 'apple', 'cherry'}

{'aaabnn', 'aelpp', 'cehrry'}

View Solution

26. Generate a set of tuples containing even and odd numbers from 1 to 10

Sample Output

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

View Solution

27. Create a set of words with their vowels replaced by underscores

Sample Output

{'banana', 'cherry', 'apple'}

{'_ppl_', 'ch_rry', 'b_n_n_'}

View Solution

28. Generate a set of tuples containing numbers and their cubes

Sample Output

{(3, 27), (4, 64), (1, 1), (5, 125), (2, 8)}

View Solution

29. Create a set of unique characters from a list of words

Sample Output

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

{'y', 'a', 'e', 'c', 'b', 'p', 'l', 'n', 'h', 'r'}

View Solution

30. Generate a set of tuples containing numbers and their absolute values

Sample Output

[-2, 3, -5, 7, -11]

{(7, 7), (-11, 11), (3, 3), (-5, 5), (-2, 2)}

View Solution

31. Create a set of words with their characters repeated twice

Sample Output

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

{'aappppllee', 'bbaannaannaa', 'cchheerrrryy'}

View Solution

32. Generate a set of tuples containing numbers and their squares, but only for even numbers

Sample Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

{(2, 4), (4, 16), (8, 64), (10, 100), (6, 36)}

View Solution

33. Create a set of numbers that are perfect cubes from 1 to 100

Sample Output

{8, 1, 27}

View Solution

34. Create a set of non-whitespace characters from a string

Sample Output

Hello, world!

{'e', 'r', 'H', 'd', 'w', ',', 'l', '!', 'o'}

View Solution

35. Create a set of even numbers from 1 to 50 that are not divisible by 4

Sample Output

{2, 34, 6, 38, 10, 42, 14, 46, 18, 50, 22, 26, 30}

View Solution

36. Generate a set of common elements from multiple sets

Sample Output

{1, 2, 3, 4, 5}

{3, 4, 5, 6, 7}

{5, 6, 7, 8, 9}

{5}

View Solution

37. Create a set of strings with their vowels removed

Sample Output

{'banana', 'cherry', 'apple'}

{'chrry', 'ppl', 'bnn'}

View Solution

38. Generate a set of words that start with a vowel from a sentence

Sample Output

This is a sample sentence with words starting with vowels.

{'a', 'is'}

View Solution

39. Create a set of unique vowels from a list of words

Sample Output

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

{'a', 'e'}

View Solution

40. Create a set of numbers that are palindromes from 1 to 100

Sample Output

{1, 2, 3, 4, 5, 6, 7, 8, 9, 33, 11, 44, 66, 77, 99, 22, 55, 88}

View Solution

41. Generate a set of words that are anagrams from a list of words

Sample Output

['apple', 'banana', 'elppa', 'race', 'care', 'cherry']

{'aaabnn', 'cehrry', 'aelpp', 'acer'}

View Solution

42. Create a set of words with their characters sorted in descending order

Sample Output

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

{'pplea', 'yrrhec', 'nnbaaa'}

View Solution

43. Generate a set of characters that appear exactly twice in a string

Sample Output

Hello, world!

{'o'}

View Solution

44. Create a set of strings with all characters capitalized from a list of strings

Sample Output

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

{'Banana', 'Apple', 'Cherry'}

View Solution

45. Generate a set of words with at least one vowel from a list of words

Sample Output

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

{'banana', 'apple', 'cherry'}

View Solution

46. Create a set of words with their characters repeated three times from a list of words

Sample Output

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

{'bbbaaannnaaannnaaa', 'aaappppppllleee', 'ccchhheeerrrrrryyy'}

View Solution

47. Create a set of words with their characters sorted and concatenated

Sample Output

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

{'aelpp', 'cehrry', 'aaabnn'}

View Solution

48. Create a set of words with their characters shuffled from a list of words

Sample Output

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

{'hreyrc', 'nbaana', 'lepap'}

View Solution

49. Filtering out negative numbers from a set

Sample Output

{1, 3, 5, -4, -2}

{1, 3, 5}

View Solution

50. Generating pairs of elements from two sets

Sample Output

{1, 2, 3}

{'b', 'c', 'a'}

{(2, 'b'), (3, 'a'), (3, 'b'), (1, 'b'), (1, 'a'), (2, 'c'), (3, 'c'), (1, 'c'), (2, 'a')}

View Solution

51. Checking for palindrome words

Sample Output

['radar', 'hello', 'level', 'world']

{'radar', 'level'}

View Solution

52. Converting list of dictionaries to a set

Sample Output

[{'a': 1, 'b': 2}, {'b': 2, 'c': 3}, {'c': 3, 'd': 4}]

{frozenset({('a', 1), ('b', 2)}), frozenset({('d', 4), ('c', 3)}), frozenset({('b', 2), ('c', 3)})}

View Solution

53. Removing punctuation from a string

Sample Output

Hello, world! How's everything?

{'e', ' ', 'g', 'o', 'h', 'd', 'l', 's', 'r', 'y', 'w', 't', 'H', 'n', 'v', 'i'}

View Solution

54. Set of words with a given prefix

Sample Output

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

ba

{'banana'}

View Solution

55. Merging characters from a list of strings

Sample Output

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

{'r', 'b', 'p', 'n', 'a', 'e', 'c', 'h', 'y', 'l'}

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial