Python : Dictionary Comprehensions - Exercises and Solution


1. Create a dictionary of squares of numbers from 1 to 10

Sample Output

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

View Solution

2. Create a dictionary of even numbers as keys and their squares as values

Sample Output

{0: 0, 2: 4, 4: 16, 6: 36, 8: 64, 10: 100, 12: 144, 14: 196, 16: 256, 18: 324, 20: 400}

View Solution

3. Create a dictionary of words and their lengths from a sentence

Sample Output

Enter String : Python is awesome

{'Python': 6, 'is': 2, 'awesome': 7}

View Solution

4. Create a dictionary of lowercase characters from a string

Sample Output

Hello World

{'H': 'h', 'e': 'e', 'l': 'l', 'o': 'o', 'W': 'w', 'r': 'r', 'd': 'd'}

View Solution

5. Create a dictionary of numbers and their cubes

Sample Output

[1, 2, 3, 4, 5]

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

View Solution

6. Create a dictionary of numbers and their squares, excluding odd numbers

Sample Output

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

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

View Solution

7. Create a dictionary of numbers and their prime status

Sample Output

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

{1: False, 2: True, 3: True, 4: False, 5: True, 6: False, 7: True, 8: False, 9: False, 10: False}

View Solution

8. Create a dictionary of characters and their ASCII values from a string

Sample Output

Tutor Joes

{'T': 84, 'u': 117, 't': 116, 'o': 111, 'r': 114, ' ': 32, 'J': 74, 'e': 101, 's': 115}

View Solution

9. Create a dictionary of words and their vowels from a list of strings

Sample Output

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

{'apple': 2, 'banana': 3, 'cherry': 1}

View Solution

10. Create a dictionary of words with the letters sorted

Sample Output

['python', 'programming', 'language']

{'python': 'hnopty', 'programming': 'aggimmnoprr', 'language': 'aaegglnu'}

View Solution

11. Create a dictionary of words and their lengths, but only for words with more than 5 letters

Sample Output

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

{'banana': 6, 'cherry': 6}

View Solution

12. Create a dictionary of characters and their frequency in a string

Sample Output

hello world

{'l': 3, ' ': 1, 'r': 1, 'h': 1, 'd': 1, 'o': 2, 'e': 1, 'w': 1}

View Solution

13. Create a dictionary of words and their reversed forms

Sample Output

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

{'apple': 'elppa', 'banana': 'ananab', 'cherry': 'yrrehc'}

View Solution

14. Create a dictionary of 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, 6: 36, 8: 64, 10: 100}

View Solution

15. Create a dictionary of numbers and their factors

Sample Output

[1, 2, 3, 4, 5]

{1: [1], 2: [1, 2], 3: [1, 3], 4: [1, 2, 4], 5: [1, 5]}

View Solution

16. Create a dictionary of words and their uppercase forms

Sample Output

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

{'apple': 'APPLE', 'banana': 'BANANA', 'cherry': 'CHERRY'}

View Solution

17. Create a dictionary of numbers and their parity (even or odd)

Sample Output

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

{1: 'odd', 2: 'even', 3: 'odd', 4: 'even', 5: 'odd', 6: 'even', 7: 'odd', 8: 'even', 9: 'odd', 10: 'even'}

View Solution

18. Create a dictionary of numbers and their binary representations

Sample Output

[1, 2, 3, 4, 5]

{1: '0b1', 2: '0b10', 3: '0b11', 4: '0b100', 5: '0b101'}

View Solution

19. Create a dictionary of numbers and their factorials

Sample Output

[1, 2, 3, 4, 5]

{1: 1, 2: 2, 3: 6, 4: 24, 5: 120}

View Solution

20. Create a dictionary of numbers and their squares, but only for multiples of 3

Sample Output

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

{3: 9, 6: 36, 9: 81}

View Solution

21. Create a dictionary of numbers and their powers of 2

Sample Output

[1, 2, 3, 4, 5]

{1: 2, 2: 4, 3: 8, 4: 16, 5: 32}

View Solution

22. Create a dictionary of words and their vowels, excluding words with no vowels

Sample Output

['apple', 'banana', 'cherry', 'dog', 'ct']

{'apple': 2, 'banana': 3, 'cherry': 1, 'dog': 1, 'cat': 1}

View Solution

23. Create a dictionary of strings by replacing vowels with underscores

Sample Output

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

{'apple': '_ppl_', 'banana': 'b_n_n_', 'cherry': 'ch_rry'}

View Solution

24. Create a dictionary of words and their lengths, but only for words starting with 'a'

Sample Output

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

{'apple': 5}

View Solution

25. Create a dictionary of strings by repeating each word thrice

Sample Output

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

{'apple': 'appleappleapple', 'banana': 'bananabananabanana', 'cherry': 'cherrycherrycherry'}

View Solution

26. Create a dictionary of strings with words containing 'a' and their lengths

Sample Output

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

{'apple': 5, 'banana': 6, 'date': 4}

View Solution

27. Create a dictionary of numbers and their squares, but only for numbers greater than 5

Sample Output

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

{6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

View Solution

28. Create a dictionary of characters and their ASCII values from a string, excluding non-alphabetic characters

Sample Output

Hello123

{'H': 72, 'e': 101, 'l': 108, 'o': 111}

View Solution

29. Create a dictionary of words and their uppercase forms, excluding words with no uppercase letters

Sample Output

['apple', 'Banana', 'cherry', 'Date']

{'Banana': 'BANANA', 'Date': 'DATE'}

View Solution

30. Create a dictionary of strings with words containing more than 4 letters

Sample Output

Python is a powerful and versatile programming language

{'Python': 6, 'powerful': 8, 'versatile': 9, 'programming': 11, 'language': 8}

View Solution

31. Create a dictionary of words and their first letter capitalized

Sample Output

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

{'apple': 'Apple', 'banana': 'Banana', 'cherry': 'Cherry'}

View Solution

32. Create a dictionary of strings with vowels removed

Sample Output

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

{'apple': 'ppl', 'banana': 'bnn', 'cherry': 'chrry'}

View Solution

33. Create a dictionary of numbers with their signs reversed

Sample Output

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

{5: -5, -10: 10, 15: -15, -20: 20, 25: -25}

View Solution

34 .Create a dictionary of strings with words in reverse

Sample Output

Python is fun

{0: 'fun', 1: 'is', 2: 'Python'}

View Solution

35. Create a dictionary of numbers with their divisors

Sample Output

[1, 2, 3, 4, 5]

{1: [1], 2: [1, 2], 3: [1, 3], 4: [1, 2, 4], 5: [1, 5]}

View Solution

36. Create a dictionary of characters and their counts, excluding whitespace characters, from a string

Sample Output

hello world

{'o': 2, 'h': 1, 'r': 1, 'w': 1, 'e': 1, 'l': 3, 'd': 1}

View Solution

37. Mapping lowercase letters to their ASCII values

Sample Output

{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104, 'i': 105, 'j': 106, 'k': 107, 'l': 108, 'm': 109, 'n': 110, 'o': 111, 'p': 112, 'q': 113, 'r': 114, 's': 115, 't': 116, 'u': 117, 'v': 118, 'w': 119, 'x': 120, 'y': 121, 'z': 122}

View Solution

38. Numbers and their factorial from 1 to 10

Sample Output

{1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: 5040, 8: 40320, 9: 362880, 10: 3628800}

View Solution

39. Numbers and their binary representation from 1 to 10

Sample Output

{1: '1', 2: '10', 3: '11', 4: '100', 5: '101', 6: '110', 7: '111', 8: '1000', 9: '1001', 10: '1010'}

View Solution

40. Pairs of distinct elements and their absolute difference from two lists

Sample Output

[3, 6, 9]

[5, 10, 15]

{(3, 5): 2, (3, 10): 7, (3, 15): 12, (6, 5): 1, (6, 10): 4, (6, 15): 9, (9, 5): 4, (9, 10): 1, (9, 15): 6}

View Solution

41. Pairs of distinct elements and their character position sum from two lists

Sample Output

['abc', 'def', 'ghi']

['jkl', 'mno', 'pqr']

{('abc', 'jkl'): 615, ('abc', 'mno'): 624, ('abc', 'pqr'): 633, ('def', 'jkl'): 624, ('def', 'mno'): 633, ('def', 'pqr'): 642, ('ghi', 'jkl'): 633, ('ghi', 'mno'): 642, ('ghi', 'pqr'): 651}

View Solution

42. Distinct words and their length, excluding those with odd lengths, in a sentence

Sample Output

Hello, how are you?

{'Hello,': 6, 'you?': 4}

View Solution

43. Distinct words and their length, excluding those with even lengths, in a sentence

Sample Output

Hello, how are you?

{'how': 3, 'are': 3}

View Solution

44. Characters and their occurrence count, excluding punctuation, in a sentence

Sample Output

Hello, how are you?

{'w': 1, 'r': 1, 'u': 1, 'o': 3, 'e': 2, 'H': 1, 'l': 2, 'h': 1, ' ': 3, 'y': 1, 'a': 1}

View Solution

45. Distinct words and their length, excluding those with lengths not Fibonacci, in a sentence

Sample Output

Hello, how are you?

{'are': 3, 'how': 3}

View Solution

46. Mapping words to their frequency of containing the letter 'e'

Sample Output

Hello, how are you?

{'Hello,': 1, 'how': 0, 'are': 1, 'you?': 0}

View Solution

47. Mapping characters to their position in the alphabet

Sample Output

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}

View Solution

48. Mapping words to their palindrome status in a sentence

Sample Output

madam sees the racecar

{'madam': True, 'sees': True, 'the': False, 'racecar': True}

View Solution

49. Mapping numbers to their binary and hexadecimal representations

Sample Output

{1: ('1', '1'), 2: ('10', '2'), 3: ('11', '3'), 4: ('100', '4'), 5: ('101', '5'), 6: ('110', '6'), 7: ('111', '7'), 8: ('1000', '8'), 9: ('1001', '9'), 10: ('1010', 'a')}

View Solution

50. Mapping words to their reverse in a sentence

Sample Output

Hello, how are you?

{'Hello,': ',olleH', 'how': 'woh', 'are': 'era', 'you?': '?uoy'}

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial