Python : List Comprehensions - Exercises and Solution


1. Create a List of Squares of numbers from 1 to 10

Sample Output

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

View Solution

2. Create a List of Even numbers from 1 to 20

Sample Output

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

View Solution

3. Generate a list of characters from a string

Sample Output

['H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

View Solution

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

Sample Output

This is a sample sentence.

[4, 2, 1, 6, 9]

View Solution

5. Generate a list of tuples containing a number and its square

Sample Output

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

View Solution

6. Create a list of lowercase letters

Sample Output

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

View Solution

7. Generate a list of uppercase letters

Sample Output

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

View Solution

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

Sample Output

[1, 4, 27, 16, 125, 36, 343, 64, 729, 100]

View Solution

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

Sample Output

[15, 30, 45, 60, 75, 90]

View Solution

10. Create a list of reversed strings from another list

Sample Output

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

['elppa', 'ananab', 'yrrehc']

View Solution

11. Generate a list of prime numbers from 1 to 50

Sample Output

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

View Solution

12. Create a list of squares of even numbers and cubes of odd numbers from -5 to 5

Sample Output

[-125, 16, -27, 4, -1, 0, 1, 4, 27, 16, 125]

View Solution

13. Generate a list of strings with their lengths from another list

Sample Output

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

[('apple', 5), ('banana', 6), ('cherry', 6)]

View Solution

14. Create a list of first characters from a list of words

Sample Output

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

['a', 'b', 'c']

View Solution

15. Generate a list of numbers with their squares if the number is even

Sample Output

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

[4, 16, 36, 64, 100]

View Solution

16. Create a list of uppercase words from a sentence

Sample Output

This is a sample sentence.

['THIS', 'IS', 'A', 'SAMPLE', 'SENTENCE.']

View Solution

17. Generate a list of strings with vowels removed

Sample Output

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

['ppl', 'bnn', 'chrry']

View Solution

18. Create a list of numbers that are divisible by both 3 and 5 from 1 to 100

Sample Output

[15, 30, 45, 60, 75, 90]

View Solution

19. Generate a list of numbers with their signs reversed

Sample Output

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

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

View Solution

20. Create a list of words with their lengths from a sentence

Sample Output

This is a sample sentence.

[('This', 4), ('is', 2), ('a', 1), ('sample', 6), ('sentence.', 9)]

View Solution

21. Generate a list of positive numbers from another list

Sample Output

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

[1, 3, 5]

View Solution

22. Generate a list of numbers that are perfect squares from 1 to 100

Sample Output

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

View Solution

23. Create a list of numbers with their absolute values

Sample Output

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

[2, 3, 5, 7, 11]

View Solution

24. Generate a list of uppercase letters using ASCII values

Sample Output

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

View Solution

25. Create a list of words with their lengths greater than 3 from a sentence

Sample Output

This is a sample sentence.

['This', 'sample', 'sentence.']

View Solution

26. Generate a list of squares of even numbers from 1 to 20

Sample Output

[4, 16, 36, 64, 100, 144, 196, 256, 324, 400]

View Solution

27. Create a list of characters and their ASCII values

Sample Output

Hello, world!

[('H', 72), ('e', 101), ('l', 108), ('l', 108), ('o', 111), (',', 44), (' ', 32), ('w', 119), ('o', 111), ('r', 114), ('l', 108), ('d', 100), ('!', 33)]

View Solution

28. Generate a list of tuples containing two numbers whose sum is even

Sample Output

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

View Solution

29. Generate a list of pairs of numbers where the sum of each pair is prime

Sample Output

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

View Solution

30. Create a list of strings with uppercase first letters

Sample Output

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

['Apple', 'Banana', 'Cherry']

View Solution

31. Generate a list of tuples containing numbers and their squares

Sample Output

[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64), (9, 81), (10, 100)]

View Solution

32. Create a list of numbers where each number is doubled

Sample Output

[1, 2, 3, 4, 5]

[2, 4, 6, 8, 10]

View Solution

33. Create a list of characters that are not alphanumeric from a string

Sample Output

Hello, world!

[',', ' ', '!']

View Solution

34. Generate a list of numbers that are powers of 2 from 1 to 10

Sample Output

[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]

View Solution

35. Create a list of strings with characters in uppercase

Sample Output

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

['APPLE', 'BANANA', 'CHERRY']

View Solution

36. Generate a list of tuples containing even and odd numbers from 1 to 10

Sample Output

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

View Solution

37. Create a list of words with their lengths from another list

Sample Output

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

[5, 6, 6]

View Solution

38. Generate a list of tuples containing numbers and their signs

Sample Output

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

[(-2, 'negative'), (3, 'positive'), (-5, 'negative'), (7, 'positive'), (-11, 'negative')]

View Solution

39. Create a list of strings with vowels replaced by asterisks

Sample Output

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

['*ppl*', 'b*n*n*', 'ch*rry']

View Solution

40. Generate a list of strings with their first letters removed

Sample Output

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

['pple', 'anana', 'herry']

View Solution

41. Create a list of numbers with their reciprocal values

Sample Output

[2, 3, 4, 5, 6]

[0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666]

View Solution

42. Generate a list of tuples containing numbers and their squares if the number is prime

Sample Output

[(2, 4), (3, 9), (5, 25), (7, 49)]

View Solution

43. Create a list of words with their characters sorted

Sample Output

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

['aelpp', 'aaabnn', 'cehrry']

View Solution

44. Generate a list of tuples containing numbers and their cubes

Sample Output

[(1, 1), (2, 8), (3, 27), (4, 64), (5, 125), (6, 216), (7, 343), (8, 512), (9, 729), (10, 1000)]

View Solution

45. Create a list of lowercase vowels from a string

Sample Output

Hello, world!

['e', 'o', 'o']

View Solution

46. Create a list of numbers with their square roots

Sample Output

[1, 4, 9, 16, 25]

[1.0, 2.0, 3.0, 4.0, 5.0]

View Solution

47. Generate a list of numbers that are palindromes from 1 to 100

Sample Output

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

View Solution

48. Create a list of numbers with their factorial values

Sample Output

[2, 3, 4, 5]

[2, 6, 24, 120]

View Solution

49. Generate a list of strings with vowels removed from a sentence

Sample Output

This is a sample sentence with some vowels.

['Ths', 's', '', 'smpl', 'sntnc', 'wth', 'sm', 'vwls.']

View Solution

50. Create a list of characters that are digits from a string

Sample Output

12345Hello67890

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

View Solution

51. List of elements with their frequency in a list

Sample Output

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

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

View Solution

52. List of words with their first and last letter swapped

Sample Output

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

['eppla', 'aananb', 'yherrc', 'eatd']

View Solution

53. List of numbers with their divisors

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

54. List of characters that are vowels or consonants

Sample Output

['a', 'b', 'c', 'e', 'f', 'i', 'o']

['a', 'e', 'i', 'o']

['b', 'c', 'f']

View Solution

55. Removing whitespace from strings in a list

Sample Output

[' hello ', ' world ', ' python ']

['hello', 'world', 'python']

View Solution

56. Create a list of characters that are not vowels from a string

Sample Output

Hello, world!

['H', 'l', 'l', ',', ' ', 'w', 'r', 'l', 'd', '!']

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial