Lesson 27 of 34

List Comprehensions: एक लाइन में Lists

8 min 30 XP

loop से list बनाना एक लाइन में लिखा जा सकता है: [expression for item in collection]। यह Python की सबसे पसंद की जाने वाली ख़ूबियों में से एक है।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

आख़िर में if जोड़कर सिर्फ़ मेल खाने वाले items रखें (filter)।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

dictionary comprehension के लिए { } का उपयोग करें। comprehension बहुत लंबा हो जाए, तो सामान्य loop पढ़ने में आसान रहता है।

Challenge

+30 XP

list comprehension से 1 से 10 तक की सम संख्याओं के वर्ग एक list में print करें: [4, 16, 36, 64, 100]

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.