List comprehension

1) Square Numbers

Create a list comprehension that produces squares of numbers 1–10.

Example output:

[1, 4, 9, …, 100]

2) Even Numbers

Use a list comprehension to create a list of even numbers from 2 to 20.


3) Odd Numbers

Generate a list of odd numbers from 1 to 19 using a comprehension.


4) Double Values

Given list [1, 2, 3, 4], use comprehension to make a new list with values doubled:

[2, 4, 6, 8]

5) Uppercase Strings

Given ["apple", "banana", "cherry"], use a list comprehension to make all uppercase.


6) Filter > 5

From list [2, 5, 8, 1, 7], use comprehension to get values greater than 5:


7) Characters of a String

Ask user for a word and use a list comprehension to create a list of its characters.


8) Lengths of Words

Given ["cat", "lion", "elephant"], make a list of word lengths:


9) Multiply Pairs

From list [1, 2, 3], create a new list with each element multiplied by 5.


10) Two Lists Combined

Given:

Use nested comprehension:


11) Positive Numbers Only

Given [-1, 3, -5, 6], use comprehension to extract positive numbers:


12) Divisible by 3

From 1–30, list numbers divisible by 3 using comprehension.


13) Strip Spaces

Given [" hello", " world "], use comprehension to strip spaces.


14) Convert Strings to Integers

Ask user for a comma-separated string of numbers (e.g., “1,2,3”). Use comprehension to make a list of ints.


15) Filter Words

Given ["apple", "dog", "cat"], get words longer than 3 letters.


16) Even Squares

Use a list comprehension to list squares of even numbers from 1–10:


17) Tuple Swap

Given list of tuples:

Use comprehension to swap each tuple:


18) Flatten Nested List

Given:

Use nested list comprehension to flatten:


19) Replace Negatives

Given [-2, 0, 3, -5], use comprehension to replace negatives with zero:


20) Length Filter + Transform

From:

Get lengths of words longer than 3:


💡 Quick Patterns

Transformation

Filtering

Nested


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated