🟢 Beginner  ·  Lesson 16

List, Set और Dictionary Comprehension

List Comprehension क्या है?

List Comprehension ka matlab hai: List comprehension creates a new list in a compact and readable way using an expression and a loop. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki filtering lists quickly jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye filtering lists quickly me kaam aata hai.
  • Ye transforming data in one line se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
compact loopcompact loop is an important term in this topic.
filterKeeps only items that satisfy a condition.
expressionA value-producing part of code, such as x * 2.
new listnew list is an important term in this topic.
readabilityThe quality of code being easy to understand for humans.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
numbers = [1, 2, 3, 4, 5, 6]
squares = [n * n for n in numbers]
even_squares = [n * n for n in numbers if n % 2 == 0]
print(squares)
print(even_squares)

Complete Example Program

Python – list-comprehension.py
numbers = [1, 2, 3, 4, 5, 6]

squares = [n * n for n in numbers]
even_squares = [n * n for n in numbers if n % 2 == 0]

print(squares)
print(even_squares)

Expected Output

[1, 4, 9, 16, 25, 36] [4, 16, 36]

Program Explanation

  • numbers = [1, 2, 3, 4, 5, 6] stores a value in numbers.
  • squares = [n * n for n in numbers] stores a value in squares.
  • even_squares = [n * n for n in numbers if n % 2 == 0] performs the next step of the program logic.
  • print(squares) displays information or calculated result on the screen.
  • print(even_squares) displays information or calculated result on the screen.

Practical Uses

  • Filtering lists quickly.
  • Transforming data in one line.
  • Writing concise readable code.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Program ko list-comprehension.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. filtering lists quickly par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

List Comprehension ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.