On this page

Lists

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

What a List Is​

A list is Python’s primary ordered, mutable data structure. It stores a sequence of items in a single variable, allowing you to group related values together. Lists can contain numbers, strings, Booleans, other lists, or any Python object.

Lists are written using square brackets:


numbers = [10, 20, 30]

words = ["apple", "banana", "cherry"]

mixed = [1, "two", True]

Lists preserve order, allow duplicates, and can grow or shrink dynamically.

Creating Lists​

You can create lists in several ways:


empty = []

values = [1, 2, 3]

letters = list("abc")     # ['a', 'b', 'c']

The list() constructor converts iterables into lists.

Accessing List Elements (Indexing)​

Each item in a list has an index, starting at 0.


items = ["a", "b", "c"]



print(items[0])   # 'a'

print(items[1])   # 'b'

Negative indices access items from the end:


print(items[-1])  # 'c'

print(items[-2])  # 'b'

Indexing is essential for retrieving specific elements.

Slicing Lists​

A slice extracts a portion of a list.


numbers = [10, 20, 30, 40, 50]



print(numbers[1:4])   # [20, 30, 40]

print(numbers[:3])    # [10, 20, 30]

print(numbers[3:])    # [40, 50]

Slicing never raises an error when the range exceeds the list length.

Modifying Lists (Mutability)​

Lists are mutable, meaning you can change their contents.

Changing an Item​


values = [1, 2, 3]

values[1] = 20

Adding Items​


values.append(4)

values.insert(1, 15)

Removing Items​


values.remove(20)

last = values.pop()     # removes and returns last item

Mutability makes lists ideal for dynamic data.

Looping Through Lists​

A for loop processes each element in order.


fruits = ["apple", "banana", "cherry"]



for f in fruits:

    print(f)

This is the most common way to work with list data.

Checking Membership​

Use the in operator to test whether a value appears in a list.


print("apple" in fruits)   # True

print("orange" in fruits)  # False

Membership checks are essential for validation and filtering.

Useful List Functions and Methods​

Python provides several built‑in tools for list manipulation.


len(values)        # number of items

min(values)        # smallest value

max(values)        # largest value

sorted(values)     # returns a new sorted list

These operations are fundamental for data processing.

Nested Lists​

A list can contain other lists, forming multi‑dimensional structures.


matrix = [

    [1, 2],

    [3, 4]

]

Access nested elements using multiple indices:


print(matrix[1][0])   # 3

Nested lists are useful for representing grids, tables, and structured data.

Why Lists Matter​

Lists are one of the most important data structures in Python. They allow you to:

Mastering lists is essential before learning tuples, dictionaries, sets, and advanced data‑handling techniques in later lessons.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Core Data Structures

Next

Tuples