Python Lists
Python Lists

Python Lists

Table of contents

  1. Creating a Python list
  2. Length of a List
  3. Accessing items of a List
    • Indexing
    • List Slicing
  4. Iterating a List
  5. Iterate along with an index number

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 item
  • item: variable name representing each element in the iterable
  • iterable: a sequence (like a list or range()) you loop through

Basic List Comprehension Examples

Python Study Note: List Comprehension (Finance Examples Only)