Curriculum / Python / Lesson 11

Lists in Python

The list is Python's most-used data structure — an ordered, mutable (changeable) collection that can hold items of any type, and grow or shrink as needed.

11 of 11
intermediate 📖 6 min read 🎯 1 question

Lists

The list is Python's most-used data structure — an ordered, mutable (changeable) collection that can hold items of any type, and grow or shrink as needed. Lists are central to almost every real Python program.

💡 Key insight Unlike a C array, a Python list can mix types and change size at any point during the program.

Syntax

syntax
myList = [item1, item2, item3]
myList.append(item)
myList.remove(item)
myList[index]
myList[start:end]

Example

.append() adds to the end, .remove() deletes the first matching item, and slicing [1:3] returns items at index 1 and 2 (not 3).

py
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
fruits.remove("banana")
print(fruits)
print("First:", fruits[0])
print("Slice:", fruits[1:3])
output['apple', 'cherry', 'mango'] First: apple Slice: ['cherry', 'mango']

Try it yourself

Copy this example into the Python compiler and run it — try changing a value and see what happens.

Quick Quiz

1. Which method adds a single item to the end of a Python list?

Back to curriculum