Python List Comprehension Basics

Day 22: List Comprehension in Python Continuing my Python learning journey. Today I explored List Comprehension, a concise way to create lists in Python. It allows us to generate a new list by applying an expression to each item in an existing iterable such as a list or range. Basic Syntax : new_list = [expression for item in iterable] Example numbers = [1, 2, 3, 4, 5] squares = [x * x for x in numbers] print(squares) Output : [1, 4, 9, 16, 25] Using Condition in List Comprehension numbers = [1, 2, 3, 4, 5, 6] even_numbers = [x for x in numbers if x % 2 == 0] print(even_numbers) Output: [2, 4, 6] List comprehension helps write cleaner and more readable Python code when working with lists. #Python #PythonLearning #CodingJourney

  • graphical user interface, application, website

To view or add a comment, sign in

Explore content categories