List comprehension is a concise and powerful way to create lists in Python. It allows us to create a new list by applying an expression to each item in an existing iterable and optionally filtering the items based on a condition.
In this tutorial, we'll learn about how to use list comprehension in Python with some examples.
List comprehensions consist of the following components:
new_list = [expression for item in iterable if condition]
new_list: The resulting list.
expression: An operation to perform on each item.
item: A variable representing an item in the iterable.
iterable: The source of items (e.g., a list or range).
condition (optional): A filter to include only specific items.
Example 1: Creating a list of squares.
squares = [x**2 for x in range(1, 6)]
print(squares)
The iterable is a range from 1 to 5.
The expression is x**2, which squares each number.
There is no condition, so all items from the range are included.
Output:
[1, 4, 9, 16, 25]
Example 2: Filtering even numbers.
squares = [x**2 for x in range(1, 6)]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)
The iterable is a list of numbers.
The expression is x, which keeps the number as is.
The condition checks if the number is even.
Output:
[1, 4, 9, 16, 25]
Trainings :
Data Science Training in Indore | Python Training in Indore | Data Analytics Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Dart | OpenCV Tutorials | Projects | Interview Questions | Python Data Structures and Algorithms | Aptitude Tests | Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | HackerRank Python | Pandas Tutorials | Projects | Interview Questions | Rust Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | MongoDB Tutorials | Examples | Interview Questions | HackerRank C Program Solutions | Python Tutorials by CodersDaily | React.js Tutorials | Golang Tutorials | Projects | Interview Questions | MS Sql Server Tutorials | Examples | Interview Questions | Verbal Ability Tutorial | Numpy Tutorials | Projects | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Tensor Flow | HackerRank C++ Solutions | Django REST Framework Tutorial | CodeChef Python Solutions | Leetcode Python Solutions | Reasoning Ability Tutorial | Quantitative Ability Tutorial | C++ Tutorials | HackerRank Java Solutions | TCS NQT Mock Test Series | Verbal Aptitude 2 | HackerRank DSA Solutions | HackerRank SQL Solutions | Javascript | Node.js Tutorials |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |