Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python List Comprehension (Complete Guide for Beginners)

List Comprehension is one of the most powerful and elegant features in Python. It allows you to create lists in a single line of code, making your programs shorter, faster, and more readable.

Instead of writing multiple lines with loops, you can generate a new list in a compact way.


🔹 What is List Comprehension?

List comprehension is a way to:

  • Create a new list
  • From an existing iterable (list, tuple, range, string, etc.)
  • Using a single line of code

🔹 Basic Syntax

new_list = [expression for item in iterable]

Breakdown:

  • expression → what you want to store in the new list
  • item → each element in the iterable
  • iterable → source data (list, range, etc.)

🔹 Example 1: Simple List Creation

Without list comprehension:

numbers = []
for i in range(5):
numbers.append(i)

print(numbers)

Output:

[0, 1, 2, 3, 4]

With list comprehension:

numbers = [i for i in range(5)]
print(numbers)

Output:

[0, 1, 2, 3, 4]

✔ Cleaner
✔ Shorter
✔ Faster


🔹 Example 2: Square Numbers

squares = [x * x for x in range(1, 6)]
print(squares)

Output:

[1, 4, 9, 16, 25]

🔹 List Comprehension with Condition

You can also add conditions (filters).

Syntax:

new_list = [expression for item in iterable if condition]

🔹 Example 3: Even Numbers Only

even_numbers = [x for x in range(1, 11) if x % 2 == 0]
print(even_numbers)

Output:

[2, 4, 6, 8, 10]

🔹 Example 4: Odd Numbers Only

odd_numbers = [x for x in range(1, 11) if x % 2 != 0]
print(odd_numbers)

Output:

[1, 3, 5, 7, 9]

🔹 Example 5: Convert Strings to Uppercase

words = ["python", "java", "c++"]
upper_words = [word.upper() for word in words]

print(upper_words)

Output:

['PYTHON', 'JAVA', 'C++']

🔹 Example 6: Using if-else in List Comprehension

You can also use if-else inside expression.

Syntax:

[true_value if condition else false_value for item in iterable]

Example: Even or Odd Label

result = ["Even" if x % 2 == 0 else "Odd" for x in range(1, 6)]
print(result)

Output:

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

🔹 Example 7: Nested List Comprehension

You can create complex structures like matrices.

matrix = [[i for i in range(3)] for j in range(3)]
print(matrix)

Output:

[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]

🔹 Real-Life Use Case

Filtering active users:

users = ["admin", "guest", "inactive", "user1"]

active_users = [user for user in users if user != "inactive"]

print(active_users)

Output:

['admin', 'guest', 'user1']

🔹 List Comprehension vs For Loop

FeatureFor LoopList Comprehension
Lines of code          More           Less
Readability          Medium           High
Performance          Slower           Faster
Usage          Complex logic           Simple transformations

🔹 When NOT to Use List Comprehension

Avoid it when:

  • Logic is too complex
  • Multiple nested conditions make it unreadable
  • Code becomes hard to understand

👉 In such cases, use a normal for loop instead.


🔹 Key Points to Remember

  • Creates lists in a single line
  • Faster and more Pythonic
  • Can include conditions
  • Supports nested loops
  • Improves code readability when used properly

🚀 Conclusion

Python list comprehension is a powerful tool that helps you write cleaner and more efficient code. It is widely used in real-world Python projects for data processing, filtering, and transformation.

Mastering it will make your Python code look more professional and modern. 




Post a Comment

0 Comments