Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education
Python programming tutorial in Hindi banner Python code learning illustration Hindi Python student coding illustration Python data code pattern background

Python Tutorial in HindiiLearn Python Programming for Free

Learn Python from Beginner to Advanced in simple Hindi.

print("Namaste Python!")
# Variable banana
naam = "Ankit"
umar = 25
 
for i in range(3):
  print("Sikho!")
 
# Output:
Sikho! Sikho! Sikho!

Why Students Learn Here

📚

Structured Tutorials

Beginner se Advanced tak step-by-step organized lessons.

💻

Practical Code

Real examples aur hands-on code jo actually samajh aaye.

🧠

Python + AI Path

Ek clear roadmap jo Python se AI tak le jaata hai.

🚀

Projects & Career

Portfolio-ready projects aur interview preparation.

Ye Kaise Kaam Karta Hai

Hindi mein Python, AI aur Data Skills seekhne ka sabse simple tareeka.

📘

Seekhein

Beginner se Advanced tak, best curated tutorials aur guides Hindi mein.

💻

Practice Karein

Real code examples aur mini-projects ke through hands-on practice karein.

💬

Judein

WhatsApp community se judkar apne doubts clear karein, akele nahi seekhenge.

Python to AI Learning Roadmap

Step-by-step learning path from Python basics to AI, Data Science and career-ready projects.

1
🐍

Python Basics

Introduction, Variables, Data Types, Operators, Input & Output, Conditions, Loops, Functions

Start Python →
2
💻

Python Intermediate

Lists, Tuples, Sets, Dictionaries, Strings, Functions, Modules, File Handling, Exception Handling

Continue Python →
3
⚙️

Advanced Python

OOP, Iterators, Generators, Decorators, Regular Expressions, APIs, Automation, Virtual Environments

Learn Advanced Python →
4
🗄️

SQL & Databases

SQL Basics, MySQL, SELECT, INSERT, UPDATE, DELETE, JOIN, GROUP BY, Database Projects

Learn SQL →
5
📊

Data Analytics

NumPy, Pandas, Data Cleaning, Data Visualization, Matplotlib, Exploratory Data Analysis

Learn Data Analytics →
6
🤖

Machine Learning

ML Basics, Supervised & Unsupervised Learning, Regression, Classification, Model Evaluation, Scikit-learn

Learn Machine Learning →
7
🧠

Artificial Intelligence

AI Fundamentals, Generative AI, AI Tools, Prompt Engineering, LLM Concepts, AI Automation

Explore AI →
8
🚀

Projects & Career

Python Projects, AI Projects, Data Projects, Portfolio, GitHub, Interview Preparation, Career Roadmap

Build Your Career →

Python List Comprehension: Write 5x Cleaner Code

Python List Comprehension

Python में किसी list से नई list बनाने के लिए अक्सर हम for loop इस्तेमाल करते हैं। लेकिन Python एक ऐसा shortcut देता है जिससे वही काम सिर्फ एक लाइन में हो जाता है — इसे List Comprehension कहते हैं।

यह Python के सबसे पसंद किए जाने वाले features में से एक है, क्योंकि यह code को छोटा, साफ और पढ़ने में आसान बना देता है। अगर आपने अभी Python Lists Complete Guide in Hindi नहीं पढ़ा है, तो पहले वो article पढ़ना बहुत जरूरी है, क्योंकि List Comprehension पूरी तरह List के ऊपर based है। Examples खुद run करने के लिए हमारा Online Python Editor & Compiler use कर सकते हैं।

🎥 Video में पूरा Topic समझें
Python List Comprehension - Write 5x Cleaner Code

List Comprehension क्या है?

List Comprehension Python का एक ऐसा तरीका है जिससे किसी existing iterable (जैसे list, range) से नई list को एक ही लाइन में बनाया जा सकता है।

सरल भाषा में — List Comprehension एक छोटा सा loop है, जो सीधे एक नई list बना देता है।
Example
squares = [x * x for x in range(1, 6)] print(squares)

Output:

[1, 4, 9, 16, 25]
Try it Yourself »

List Comprehension की जरूरत क्यों पड़ती है?

बिना List Comprehension के, किसी list से नई list बनाने के लिए हमें कई लाइनों का loop लिखना पड़ता है।

Normal Loop से
squares = [] for x in range(1, 6): squares.append(x * x) print(squares)
List Comprehension से — same result
squares = [x * x for x in range(1, 6)] print(squares)

दोनों का output बिल्कुल same है — [1, 4, 9, 16, 25] — लेकिन List Comprehension सिर्फ एक लाइन में, छोटा और साफ है।

Basic List Comprehension Syntax

Syntax
new_list = [expression for item in iterable]
  • expression → हर item पर जो operation करना है
  • item → iterable का हर एक value, एक-एक करके
  • iterable → list, range, या कोई भी loop होने लायक चीज़

Expression और Iterable

[ x * x for x in range(1, 6) ] ↑ ↑ ↑ Expression Item Iterable
Example
doubled = [x * 2 for x in [1, 2, 3, 4]] print(doubled)

Output:

[2, 4, 6, 8]

यहाँ x * 2 expression है, x हर item है, और [1, 2, 3, 4] iterable है।

Try it Yourself »

Numbers के साथ List Comprehension

Example
numbers = [x for x in range(1, 6)] print(numbers)

Output:

[1, 2, 3, 4, 5]
Try it Yourself »

Existing List से नई List बनाना

Example
fruits = ["apple", "banana", "mango"] upper_fruits = [fruit.upper() for fruit in fruits] print(upper_fruits)

Output:

['APPLE', 'BANANA', 'MANGO']

यहाँ एक existing list fruits से नई list upper_fruits बनाई गई, जिसमें हर नाम uppercase में है।

Try it Yourself »

Calculation के साथ List Comprehension

Example
prices = [100, 200, 300] with_tax = [price + (price * 0.18) for price in prices] print(with_tax)

Output:

[118.0, 236.0, 354.0]

हर price पर 18% tax जोड़कर नई list बनाई गई — बिना किसी extra loop के।

Try it Yourself »

if Condition के साथ List Comprehension

List Comprehension में if लगाकर सिर्फ वही values चुनी जा सकती हैं जो किसी condition को पूरा करती हों।

Syntax
new_list = [expression for item in iterable if condition]
Example
numbers = [1, 2, 3, 4, 5, 6, 7, 8] greater_than_four = [x for x in numbers if x > 4] print(greater_than_four)

Output:

[5, 6, 7, 8]
Try it Yourself »

Even और Odd Numbers निकालना

Example — Even Numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8] even_numbers = [x for x in numbers if x % 2 == 0] print(even_numbers)

Output:

[2, 4, 6, 8]
Try it Yourself »
Example — Odd Numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8] odd_numbers = [x for x in numbers if x % 2 != 0] print(odd_numbers)

Output:

[1, 3, 5, 7]
Try it Yourself »

if-else के साथ List Comprehension

अगर हर element के लिए कोई-न-कोई value चाहिए (filter नहीं करना, बल्कि बदलना है), तो if-else को expression वाली जगह पर लिखा जाता है।

Syntax
new_list = [value_if_true if condition else value_if_false for item in iterable]
Example
numbers = [1, 2, 3, 4, 5, 6] labels = ["Even" if x % 2 == 0 else "Odd" for x in numbers] print(labels)

Output:

['Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even']

यहाँ हर number को filter नहीं किया गया, बल्कि उसकी जगह "Even" या "Odd" label डाल दिया गया।

Try it Yourself »
Note: सिर्फ filter करने के लिए if को comprehension के आखिर में लिखा जाता है, लेकिन हर element को transform करने के लिए if-else को expression के साथ, शुरुआत में लिखा जाता है — दोनों का position अलग-अलग है।

Practical Marks Example

Example
marks = [45, 78, 32, 90, 65, 20, 88] # Pass students (marks >= 40) passed = [m for m in marks if m >= 40] # Har mark par 5 bonus marks with_bonus = [m + 5 for m in passed] # Pass/Fail label result = ["Pass" if m >= 40 else "Fail" for m in marks] print("Passed Marks:", passed) print("With Bonus:", with_bonus) print("Result Labels:", result)

Output:

Passed Marks: [45, 78, 90, 65, 88] With Bonus: [50, 83, 95, 70, 93] Result Labels: ['Pass', 'Pass', 'Fail', 'Pass', 'Pass', 'Fail', 'Pass']

यहाँ filtering, transformation और if-else — तीनों तरह के List Comprehension एक साथ इस्तेमाल किए गए हैं।

Try it Yourself »

Normal Loop vs List Comprehension

Normal LoopList Comprehension
कई लाइनों में लिखा जाता हैएक ही लाइन में लिखा जाता है
Empty list बनाकर append() करना पड़ता हैDirectly नई list return होती है
पढ़ने में ज्यादा समय लगता हैछोटे operations के लिए जल्दी पढ़ा जा सकता है
Complex logic के लिए बेहतरSimple transformations/filtering के लिए बेहतर
Example — दोनों का Comparison
numbers = [1, 2, 3, 4, 5] # Normal Loop squares_loop = [] for x in numbers: squares_loop.append(x * x) # List Comprehension squares_comp = [x * x for x in numbers] print(squares_loop) print(squares_comp)

Output:

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

दोनों का result same है, लेकिन squares_comp सिर्फ एक लाइन में लिखा गया।

Try it Yourself »

Common Beginner Mistakes

❌ Square Brackets भूल जाना
squares = x * x for x in range(5) # SyntaxError
✅ सही तरीका
squares = [x * x for x in range(5)]
❌ if और if-else का Position गलत रखना
labels = [x for x in numbers if x % 2 == 0 else "Odd"] # SyntaxError
✅ सही तरीका
# सिर्फ filter: if आखिर में even = [x for x in numbers if x % 2 == 0] # transform: if-else expression के साथ labels = ["Even" if x % 2 == 0 else "Odd" for x in numbers]
❌ बहुत Complex Logic को एक लाइन में ठूसना

अगर logic में कई conditions या steps हों, तो List Comprehension पढ़ने में मुश्किल हो जाता है — ऐसे में normal for loop ज्यादा readable रहता है।

❌ Iterable की जगह गलत variable देना
squares = [x * x for y in range(5)] # NameError: x is not defined

Expression और loop variable का नाम match होना चाहिए — यहाँ x कहीं भी for के साथ define नहीं हुआ।

Exercise ?

List Comprehension में सिर्फ filter करने के लिए if को कहाँ लिखा जाता है?

Expression के साथ, शुरुआत में
Comprehension के आखिर में
for से पहले

Coding Challenge और Final Revision

अब खुद अपने Online Python Editor में इन challenges को solve कीजिए:

  1. List Comprehension से 1 से 10 तक के numbers की cube वाली एक list बनाइए।
  2. किसी list में से सिर्फ 3 से divide होने वाले numbers चुनिए।
  3. एक list of names को List Comprehension से lowercase में बदलिए।
  4. List Comprehension के साथ if-else इस्तेमाल करके किसी list के हर number को "Positive" या "Negative" label दीजिए।
  5. किसी list of marks में से pass students (>= 35) चुनकर, उन सबमें 10 bonus marks जोड़िए — सब कुछ List Comprehension से।

Quick Revision

ConceptSyntax
Basic Comprehension[expression for item in iterable]
Filter के साथ[expression for item in iterable if condition]
Transform (if-else)[true_value if condition else false_value for item in iterable]

FAQ

Python में List Comprehension क्या है?

List Comprehension एक compact syntax है जिससे किसी iterable से नई list को एक ही लाइन में, loop इस्तेमाल करके बनाया जा सकता है।

List Comprehension और Normal Loop में क्या फर्क है?

दोनों से same काम हो सकता है, लेकिन List Comprehension इसे एक ही लाइन में, बिना append() इस्तेमाल किए करता है, जिससे code छोटा और readable बनता है।

List Comprehension में if और if-else में क्या फर्क है?

सिर्फ if (आखिर में) elements को filter करता है — सिर्फ matching elements रहते हैं। if-else (expression के साथ) हर element को transform करता है — कोई element हटता नहीं, बस उसकी value बदल जाती है।

क्या List Comprehension हमेशा Normal Loop से बेहतर है?

नहीं। छोटे और simple operations के लिए List Comprehension बेहतर है, लेकिन अगर logic complex हो या कई steps में फैला हो, तो normal for loop ज्यादा readable रहता है।

List Comprehension सिर्फ Lists के लिए है?

List Comprehension नई list बनाता है, लेकिन इसका iterable कोई भी चीज़ हो सकती है जिस पर loop चलाया जा सके — जैसे range, string, tuple, या कोई और existing list।

Summary

List Comprehension = loop + append() का काम, सिर्फ एक लाइन में।

इस article में आपने सीखा कि List Comprehension क्या है, इसका basic syntax क्या है, expression और iterable का क्या role है, if से values कैसे filter करें, if-else से values को कैसे transform करें, और Normal Loop व List Comprehension में क्या फर्क है।

List Comprehension Python का एक बहुत powerful feature है जो code को छोटा और साफ बनाता है, लेकिन इसे सिर्फ simple operations के लिए इस्तेमाल करना चाहिए। अब ऊपर दिया गया coding challenge खुद अपने Online Editor में solve करें।

Related Articles

🧑‍💻 Python Coding Lab Python code likho, run karo aur practice karo — directly browser mein.