TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4462 2.88K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 8: Python List Comprehensions

Welcome back! ๐Ÿ‘‹

In the previous lesson, you learned about Python's built-in data structuresโ€”Lists, Tuples, Sets, and Dictionaries.

Now it's time to learn one of Python's most elegant and frequently used features: List Comprehensions.

List comprehensions provide a concise and readable way to create, filter, and transform lists. They are widely used in Data Science, Machine Learning, data preprocessing, and coding interviews.

๐Ÿ”น 1. What is a List Comprehension?

A list comprehension is a compact way to create a new list by applying an expression to each item in an iterable (such as a list, tuple, or range).

Instead of writing multiple lines with a loop, you can accomplish the same task in a single line.

General Syntax

new_list = [expression for item in iterable]

๐Ÿ”น 2. Creating a List Using a Loop

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


Output

[0, 1, 2, 3, 4]


๐Ÿ”น 3. Creating the Same List Using List Comprehension

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


Output

[0, 1, 2, 3, 4]  


Notice how the code is shorter and easier to read.

๐Ÿ”น 4. Performing Calculations

Create a list of squares.

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


Output

[1, 4, 9, 16, 25]


๐Ÿ”น 5. Using Conditions

You can filter elements while creating a list.

Example: Even Numbers

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


Output

[2, 4, 6, 8, 10]


๐Ÿ”น 6. Converting Strings

Convert all names to uppercase.

names = ["rahul", "deepak", "anita"]
upper_names = [name.upper() for name in names]
print(upper_names)


Output

['RAHUL', 'DEEPAK', 'ANITA']


๐Ÿ”น 7. Using Conditional Expressions

Replace negative numbers with zero.

numbers = [5, -2, 8, -1, 3]
updated = [0 if x < 0 else x for x in numbers]
print(updated)


Output

[5, 0, 8, 0, 3]


๐Ÿ”น 8. Nested List Comprehension

Create a multiplication table.

table = [[i * j for j in range(1, 6)] for i in range(1, 4)]
print(table)


Output

[[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15]]


๐Ÿ”น 9. Real-World Data Science Example

Suppose you have a list of sales amounts.

sales = [1200, 850, 1500, 600, 2000]
high_sales = [sale for sale in sales if sale > 1000]
print(high_sales)


Output

[1200, 1500, 2000] 


This technique is commonly used while cleaning and filtering datasets before analysis.

๐Ÿ”น 10. Benefits of List Comprehensions

โœ… Shorter code

โœ… Easier to read

โœ… Faster than traditional loops in many cases

โœ… Widely used in Data Science and Machine Learning

๐Ÿ”น 11. Common Mistakes

โŒ Forgetting the Expression

numbers = [for i in range(5)]  # SyntaxError


Correct:

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


โŒ Incorrect Order of "if"

numbers = [if x % 2 == 0 x for x in range(10)]  # SyntaxError


Correct:

numbers = [x for x in range(10) if x % 2 == 0]
  • โค 9
  • ๐Ÿ‘ 1
More from @datasciencefun
  1. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  2. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Oct 2, 2026Data Visualisation tips for beginners
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’