Skip to content

Module 8 · Python Data Structures — topic 2 of 22

Lists

The list is Python’s general-purpose ordered, mutable collection. Learn what lists can hold, how they grow and shrink, common built-in operations, and how lists behave when shared between names.

Beginner 12 min read·Lesson 105 of 306 in Python Programming

Learning objectives

  • Describe the properties of lists
  • Apply len, in, +, *, min, max and sum to lists
  • Explain shared references to the same list

Definition

The list is Python’s general-purpose ordered, mutable collection. Learn what lists can hold, how they grow and shrink, common built-in operations, and how lists behave when shared between names.

Introduction

A list is an ordered collection of items that can change: you can add, remove and replace items at any time. Lists are written with square brackets and commas: ["Maths", "Physics", "Chemistry"].

They are the most frequently used collection in Python, the default choice whenever you need a sequence of things.

Why it matters

Marks of a class, lines of a file, items in a cart, results of a search: whenever the number of items can vary and their order matters, a list is the natural home for them.

Detailed explanation

Lists keep items in the order you add them, and every item has an index starting at 0.

Lists can contain duplicates and items of different types, although lists of one kind of item are easier to work with: [72, 88, 91] rather than [72, "Asha", True].

Lists are mutable. Methods such as append() and remove() change the list itself, and assignment to an index replaces an item.

A list stores references to objects, not copies. Assigning a list to another name shares the same list, so changes through one name are visible through the other. Use list.copy() or slicing [:] for an independent copy.

Built-in functions work on lists: len(), min(), max(), sum(), sorted(), any(), all().

Operators: + joins two lists into a new list, * repeats a list, in tests membership, and == compares contents item by item.

Python lists grow automatically; you never declare a size in advance.

Syntax

Python
items = [item1, item2, item3]
empty = []
length = len(items)
combined = items + more_items
  • Square brackets with comma-separated items.
  • [] is an empty list.
  • Lists can be combined, measured and searched.

Code examples

Creating and inspecting

Python
subjects = ["Maths", "Physics", "Chemistry"]
print(subjects)
print(len(subjects), subjects[0], subjects[-1])
print("Physics" in subjects)
Output
['Maths', 'Physics', 'Chemistry']
3 Maths Chemistry
True

Built-ins and operators

Python
marks = [72, 88, 91]
print(sum(marks), min(marks), max(marks))
print(marks + [65])
print([0] * 3)
Output
251 72 91
[72, 88, 91, 65]
[0, 0, 0]

More examples

Shared lists and copies

Python
today = ["milk", "bread"]
shopping = today
backup = today.copy()
shopping.append("eggs")
print("today:", today)
print("backup:", backup)
print(shopping is today, backup is today, backup == ["milk", "bread"])
Output
today: ['milk', 'bread', 'eggs']
backup: ['milk', 'bread']
True False True

Step-by-step explanation

  • shopping = today makes a second name for the same list object.
  • today.copy() creates a new list with the same items.
  • Appending through shopping changes the shared list, so today shows eggs too.
  • is confirms which names refer to the same object; == compares contents.

Expected output

Running “Shared lists and copies” prints exactly:

TextOutput
today: ['milk', 'bread', 'eggs']
backup: ['milk', 'bread']
True False True

Practical applications

  • To-do apps keep tasks in a list in the order they were added.
  • Music players store playlists as lists of song IDs.
  • Data scripts load each row of a CSV file into a list.

Best practices

  • Keep each list to one kind of item.
  • Name lists in the plural: students, prices.
  • Copy explicitly when you need an independent list.

Common mistakes

Copying with =

Avoid

backup = scores

Prefer

backup = scores.copy()

= only creates another name for the same list.

Mixing unrelated types

Avoid

student = ["Asha", 82, "Pune"]

Prefer

student = ("Asha", 82, "Pune") # or a dict

A fixed record of different fields fits a tuple or dictionary better than a list.

Key takeaways

  • Lists are ordered, mutable and allow duplicates.
  • len, in, +, *, sum, min and max work on lists.
  • Assigning a list shares it; copy() makes an independent list.

Try it yourself

  1. 1
    Create a list of five temperatures and print the highest, lowest and average.
  2. 2
    Show with is and == the difference between a shared list and a copied one.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

13 questions cover the “Python Data Structures” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
Practise this module