Comprehensions: tidy transformations
A list comprehension builds a new list from an iterable. Read it as: transform each item, optionally keeping only matching ones. Prefer a normal loop when the expression gets hard to read.
हिन्दी में समझें
लिस्ट कॉम्प्रिहेंशन से पुराने मानों से नई लिस्ट बनती है। हर मान पर क्रिया और वैकल्पिक शर्त होती है। समझने में कठिन हो तो सामान्य लूप चुनें।
अभ्यास: squares बढ़ाएँ ताकि अंतिम मान 25 हो।
Hinglish mein samjhein
Comprehension se items ko transform karke nayi list banti hai. Optional condition se filter kar sakte ho. Zyada complex ho to normal loop likho.
Practice: squares extend karo taaki last value 25 ho.
A small Python example
squares = [n * n for n in range(5)]
evens = [n for n in range(6) if n % 2 == 0]
print(squares)
print(evens)The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.
Try this example in the Python playground →Practise this idea
Extend squares so it includes 25 as the last value.
Check answer & explanation
squares = [n * n for n in range(6)]
evens = [n for n in range(6) if n % 2 == 0]
print(squares)
print(evens)range(6) includes 5. Squaring each value therefore extends the list through 5 * 5, which is 25.
Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.
Check your understanding
Which values are even?
Check the quiz answer
[0, 2, 4]
range(6) stops before 6, and 0 is even.
References & corrections
Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.