← Back to Chapters

Python Lists

? Python Lists

⚡ Quick Overview

A list in Python is a collection of items that are:

  • Ordered – items have a fixed position (index).
  • Mutable – you can change, add, or remove items.
  • Allow duplicates – the same value can appear multiple times.

Lists are one of the most flexible and commonly used data structures in Python.

? Key Concepts

  • Indexing: access items using their position (starting from 0).
  • Negative Indexing: access from the end using -1, -2, etc.
  • Slicing: get a sub-part of the list using [start:stop:step].
  • Mutability: update existing items or change the length of the list.
  • Built-in Methods: append(), insert(), remove(), pop(), sort(), reverse(), etc.

? Syntax & Basic Creation

You define a list using square brackets [] and separate items with commas.

? View Basic List Creation Example
fruits = ["apple", "banana", "cherry"]
print(fruits)
print(type(fruits))

Here, fruits is a list containing three string items. The type() function confirms that its type is list.

? Accessing & Slicing List Items

? Accessing Items by Index

You can access items in a list by index. Indexing starts at 0. Negative indices start from the end.

? View Access Example
fruits = ["apple", "banana", "cherry"]
print(fruits[0])    # apple
print(fruits[-1])   # cherry

✂️ Slicing a List

Slicing lets you extract a range of items from a list using the syntax list[start:stop:step].

? View Slicing Example
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4])   # [1, 2, 3]
print(numbers[:3])    # [0, 1, 2]
print(numbers[::2])   # [0, 2, 4]

If you leave out start, Python assumes the beginning of the list. If you leave out stop, it assumes the end. The step controls how many items to skip each time.

?️ Modifying Lists & Useful Methods

Lists are mutable, which means you can change individual elements or modify the list length.

? View Modify Example
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"
fruits.append("orange")
fruits.remove("apple")
print(fruits)

Common list methods include:

  • append(x) – add an item x at the end.
  • insert(i, x) – insert x at index i.
  • remove(x) – remove the first occurrence of x.
  • pop(i) – remove and return the item at index i (last item if index omitted).
  • sort() – sort the list in ascending order.
  • reverse() – reverse the order of the list.
? View Sorting Example
nums = [3, 1, 4, 2]
nums.sort()
print(nums)   # [1, 2, 3, 4]

? When to Use Lists

Use a list when you need:

  • A collection of items where order matters.
  • To frequently add, remove, or update elements.
  • To store mixed data types (numbers, strings, even other lists).

? Output & Explanation

?️ Sample Output for Basic List

If you run the basic list example:

fruits = ["apple", "banana", "cherry"]
print(fruits)
print(type(fruits))

You will see output like:

['apple', 'banana', 'cherry']
<class 'list'>

The first line prints the list contents. The second line confirms that fruits is an object of type list.

? Tips & Best Practices

  • Use slicing [:] to copy a list without affecting the original.
  • Use len() to quickly find the number of items in a list.
  • Lists can contain different data types, including other lists (nested lists).
  • When you don't want the data to change, consider using a tuple instead of a list.

? Try It Yourself

  • Create a list of your 5 favorite movies and print them.
  • Replace the second item in a list with a new value.
  • Use slicing to print only the first three items of a list.
  • Sort a list of numbers in ascending and descending order.