Table of contents
- Creating a Python list
- Length of a List
- Accessing items of a List
- Indexing
- List Slicing
- Iterating a List
- Iterate along with an index number
- Adding elements to the list
- Append item at the end of the list
- Add item at the specified position in the list
- Using extend()
- Modify the items of a List
- Removing elements from a List
- Finding an element in the list
- Concatenation of two lists
- Copying a list
- List operations
- Python Built-in functions with List
- Nested List
- List Comprehension
- Summary of List operations
‣
Introduction
‣
1.0 Creating a Python list
‣
2.0 Length of a List
‣
3.0 Accessing items of a List
‣
4.0 Adding elements to the list
‣
5.0 Modify the items of a List
‣
6.0 Removing elements from a List
‣
7.0 Finding an element in the list
‣
8.0 Concatenation of two lists
‣
9.0 Copy a list Function
‣
10.0 Python Built
-in functions with List
‣
11.0 The all()
and any()
Functions in Python
‣
12.0 Nested List
13.0 List Comprehension
List comprehension is a concise way to create lists in Python. It allows you to generate a new list by performing an operation on each item in an existing iterable (like a list, tuple, or range), in a single line of code.
Why Use List Comprehension?
- Shorter Code – You can achieve in one line what usually takes multiple lines.
- More Readable – The syntax is cleaner and easier to understand.
- Faster Execution – List comprehension runs faster than traditional loops.
- Pythonic Approach – It follows Python’s principle of simplicity and efficiency.
Syntax of List Comprehension
[expression for item in iterable]
expression
: what you want to do with each itemitem
: variable name representing each element in the iterableiterable
: a sequence (like a list orrange()
) you loop through
‣
Basic List Comprehension Examples
‣