Which Python data structure is ordered, mutable, and allows duplicate values?
Anonymous Quiz
9%
A) Set
24%
B) Tuple
59%
C) List
8%
D) Dictionary
๐2โค1
Which data structure stores only unique elements?
Anonymous Quiz
8%
A) List
29%
B) Tuple
45%
C) Set
19%
D) Dictionary
๐2โค1
Which statement about tuples is correct?
Anonymous Quiz
10%
A) Tuples are mutable.
25%
B) Tuples do not allow duplicate values.
61%
C) Tuples are immutable.
3%
D) Tuples are unordered.
โค4
What will be the output of the following code?
numbers = {1, 2, 2, 3, 4, 4}
print(len(numbers))
numbers = {1, 2, 2, 3, 4, 4}
print(len(numbers))
Anonymous Quiz
33%
4
15%
5
50%
6
2%
7
โค5
๐๐ & ๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ (๐ก๐ผ ๐๐ผ๐ฑ๐ถ๐ป๐ด ๐ก๐ฒ๐ฒ๐ฑ๐ฒ๐ฑ)
Apply Now๐:- https://pdlink.in/4aYWald
By E&ICT Academy, IIT Roorkee
Batch Closing Soon - 26th July 2026
Apply Now๐:- https://pdlink.in/4aYWald
By E&ICT Academy, IIT Roorkee
Batch Closing Soon - 26th July 2026
๐ 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
๐น 2. Creating a List Using a Loop
Output
๐น 3. Creating the Same List Using List Comprehension
Output
Notice how the code is shorter and easier to read.
๐น 4. Performing Calculations
Create a list of squares.
Output
๐น 5. Using Conditions
You can filter elements while creating a list.
Example: Even Numbers
Output
๐น 6. Converting Strings
Convert all names to uppercase.
Output
๐น 7. Using Conditional Expressions
Replace negative numbers with zero.
Output
๐น 8. Nested List Comprehension
Create a multiplication table.
Output
๐น 9. Real-World Data Science Example
Suppose you have a list of sales amounts.
Output
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
Correct:
โ Incorrect Order of "if"
Correct:
๐ 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]
โค7๐1
๐ฏ Practice Questions
1. Create a list of numbers from 1 to 20.
2. Create a list containing the squares of numbers from 1 to 10.
3. Create a list containing only odd numbers from 1 to 20.
4. Convert a list of names to lowercase.
5. Replace all negative values in a list with zero using a list comprehension.
๐ฏ Key Takeaways
โ List comprehensions provide a concise way to create lists.
โ They combine loops and expressions into a single line.
โ You can filter data using "if" conditions.
โ Conditional expressions allow values to be modified during list creation.
โ List comprehensions are widely used in data cleaning, feature engineering, and machine learning workflows.
Mastering list comprehensions will help you write cleaner, more Pythonic code and prepare you for technical interviews and real-world Data Science projects.
Double Tap โค๏ธ For Part-9
1. Create a list of numbers from 1 to 20.
2. Create a list containing the squares of numbers from 1 to 10.
3. Create a list containing only odd numbers from 1 to 20.
4. Convert a list of names to lowercase.
5. Replace all negative values in a list with zero using a list comprehension.
๐ฏ Key Takeaways
โ List comprehensions provide a concise way to create lists.
โ They combine loops and expressions into a single line.
โ You can filter data using "if" conditions.
โ Conditional expressions allow values to be modified during list creation.
โ List comprehensions are widely used in data cleaning, feature engineering, and machine learning workflows.
Mastering list comprehensions will help you write cleaner, more Pythonic code and prepare you for technical interviews and real-world Data Science projects.
Double Tap โค๏ธ For Part-9
โค8
๐ ๐๐ถ๐๐ฐ๐ผ ๐๐ฅ๐๐ ๐ง๐ฒ๐ฐ๐ต ๐๐ผ๐๐ฟ๐๐ฒ๐ | ๐ฑ ๐ ๐๐๐-๐๐ผ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐
Cisco offers learning opportunities covering some of the most valuable foundations for careers in Cybersecurity, Networking, Linux and IoT.
โ Beginner-Friendly Tech Skills
โ Learn In-Demand IT Concepts
โ Build Practical Knowledge
โ Strengthen Your Resume
โ Great for Students & Freshers
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4fhCSKo
๐ฅ Learn from Cisco โข Build Skills โข Upgrade Your Resume โข Get Career-Ready!
Cisco offers learning opportunities covering some of the most valuable foundations for careers in Cybersecurity, Networking, Linux and IoT.
โ Beginner-Friendly Tech Skills
โ Learn In-Demand IT Concepts
โ Build Practical Knowledge
โ Strengthen Your Resume
โ Great for Students & Freshers
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4fhCSKo
๐ฅ Learn from Cisco โข Build Skills โข Upgrade Your Resume โข Get Career-Ready!
โค2
๐ฐ 3 years of experience. Still waiting for a salary jump?
Experience alone doesn't guarantee growth.
Learning in-demand skills can help you stay competitive in today's job market.
That's why people are joining E&ICT Academy IIT Roorkee's AI & ML Program.
โ 6 Months | Online | Open for all backgrounds
โ Learn from IIT professors & industry mentors
โ Flipkart & Mamaearth projects
โ Placement support through Masai's network of 5000+ companies
๐ Entrance Test: 26th July
๐ https://tinyurl.com/DS-26Jul-006
Experience alone doesn't guarantee growth.
Learning in-demand skills can help you stay competitive in today's job market.
That's why people are joining E&ICT Academy IIT Roorkee's AI & ML Program.
โ 6 Months | Online | Open for all backgrounds
โ Learn from IIT professors & industry mentors
โ Flipkart & Mamaearth projects
โ Placement support through Masai's network of 5000+ companies
๐ Entrance Test: 26th July
๐ https://tinyurl.com/DS-26Jul-006
โค5
๐ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐ฆ๐ค๐ ๐๐ผ๐ฟ ๐๐ฅ๐๐! ๐๏ธ๐ป
Start learning SQL with these 100% FREE resources and build one of the most in-demand skills in tech!
โ Beginner-Friendly SQL Tutorials
โ FREE Online SQL Courses
โ Interactive SQL Practice Platforms
โ Real-World Database Projects
โ Interview Preparation Resources
โ Hands-on Exercises & Challenges
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4yLrNci
๐ Start your SQL journey today and unlock exciting career opportunities!
Start learning SQL with these 100% FREE resources and build one of the most in-demand skills in tech!
โ Beginner-Friendly SQL Tutorials
โ FREE Online SQL Courses
โ Interactive SQL Practice Platforms
โ Real-World Database Projects
โ Interview Preparation Resources
โ Hands-on Exercises & Challenges
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4yLrNci
๐ Start your SQL journey today and unlock exciting career opportunities!
โค4
Data Science & Machine Learning
๐ฐ 3 years of experience. Still waiting for a salary jump? Experience alone doesn't guarantee growth. Learning in-demand skills can help you stay competitive in today's job market. That's why people are joining E&ICT Academy IIT Roorkee's AI & ML Program.โฆ
Last 6 Hours Remaining!
Before the application closes for E&ICT IIT Roorkee AI & ML Program.
Don't miss out on the chance to:
โข Learn live from IIT professors & industry experts
โข Build real AI projects
โข Get Placement Support from Masai.
Register NOW
Before the application closes for E&ICT IIT Roorkee AI & ML Program.
Don't miss out on the chance to:
โข Learn live from IIT professors & industry experts
โข Build real AI projects
โข Get Placement Support from Masai.
Register NOW
โค2๐1
๐ ๐๐๐ฏ๐ฒ๐ฟ๐๐ฒ๐ฐ๐๐ฟ๐ถ๐๐ & ๐๐น๐ผ๐๐ฑ ๐๐ผ๐บ๐ฝ๐๐๐ถ๐ป๐ด ๐๐ฅ๐๐ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐๐ผ๐๐ฟ๐๐ฒ๐
Build job-ready skills in two of the most in-demand technology fields and strengthen your rรฉsumรฉ with valuable certifications! ๐
๐ Cyber Security :- https://pdlink.in/4bHIF9K
โ
โ๏ธ Cloud Computing :- https://pdlink.in/4yXs8bU
โ
Perfect for Students, Freshers & Working Professionals looking to launch or upgrade their tech careers. ๐ผ
๐ Enroll for FREE & Get Certified
Build job-ready skills in two of the most in-demand technology fields and strengthen your rรฉsumรฉ with valuable certifications! ๐
๐ Cyber Security :- https://pdlink.in/4bHIF9K
โ
โ๏ธ Cloud Computing :- https://pdlink.in/4yXs8bU
โ
Perfect for Students, Freshers & Working Professionals looking to launch or upgrade their tech careers. ๐ผ
๐ Enroll for FREE & Get Certified
โค1