Python Lists
Work with ordered, changeable collections of values.
When writing Python programs, you often need to store multiple values together. You may want to keep student names, product prices, or daily tasks. Instead of creating a separate variable for each value, Python provides a powerful data structure called a list.
A list stores multiple items in a single variable. It is one of Python's most commonly used data types because it is flexible, easy to use, and supports many useful operations.
In this lesson, you will learn how to create lists, access their elements, modify them, and use common list methods.
What is a List?
A list is an ordered collection of items. Items are enclosed in square brackets and separated by commas. A list can store numbers, strings, booleans, and even other lists.
fruits = ["Apple", "Banana", "Orange"]
print(fruits)Output
['Apple', 'Banana', 'Orange']Lists are ordered, which means each item has a specific position called an index.
Creating a List
Create a list with one or more items. Python allows different data types to exist in the same list.
numbers = [10, 20, 30, 40]
names = ["Alice", "John", "Emma"]
mixed = ["Python", 3.11, True]Accessing List Items
Access a list item using its index. Python indexes begin at zero.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
print(fruits[2])Output
Apple
OrangeNegative indexing accesses items from the end of a list. An index of -1 refers to the last item.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[-1])Output
OrangeList Slicing
Slicing extracts a portion of a list. The starting index is included, while the ending index is excluded.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])Output
[20, 30, 40]Modifying List Items
Unlike strings and tuples, lists are mutable, so their contents can be changed after creation.
fruits = ["Apple", "Banana", "Orange"]
fruits[1] = "Mango"
print(fruits)Output
['Apple', 'Mango', 'Orange']This makes lists useful when stored data needs to be updated.
Adding Items to a List
Using append()
append() adds one item to the end of a list.
fruits = ["Apple", "Banana"]
fruits.append("Orange")
print(fruits)Using insert()
insert() adds an item at a specific index.
fruits = ["Apple", "Orange"]
fruits.insert(1, "Banana")
print(fruits)Using extend()
extend() adds every item from another iterable.
list1 = [1, 2]
list2 = [3, 4]
list1.extend(list2)
print(list1)Removing Items from a List
Using remove()
remove() deletes the first matching value.
fruits = ["Apple", "Banana", "Orange"]
fruits.remove("Banana")
print(fruits)Using pop()
pop() removes and returns an item by index. Without an index, it removes the last item.
numbers = [10, 20, 30]
numbers.pop(1)
print(numbers)Using clear()
clear() removes every item from the list.
numbers = [1, 2, 3]
numbers.clear()
print(numbers)Looping Through a List
A for loop is commonly used to access and process each list item.
fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits:
print(fruit)Output
Apple
Banana
OrangeUseful List Methods
| Method | Description |
|---|---|
| append() | Adds an item to the end |
| insert() | Inserts an item at a specific position |
| extend() | Adds items from another iterable |
| remove() | Removes a specified item |
| pop() | Removes and returns an item by index |
| clear() | Removes all items |
| sort() | Sorts the list |
| reverse() | Reverses the list in place |
| copy() | Creates a shallow copy of the list |
| count() | Counts how many times an item appears |
| index() | Returns the index of the first matching item |
Sorting a List
numbers = [50, 10, 30, 20]
numbers.sort()
print(numbers)Output
[10, 20, 30, 50]List Comprehension
A list comprehension provides a concise way to create a new list from an iterable.
squares = [number * number for number in range(1, 6)]
print(squares)Output
[1, 4, 9, 16, 25]Beginners can create lists with ordinary loops first; comprehensions become especially useful as your Python skills grow.
Finding the Length of a List
The len() function returns the number of items in a list.
fruits = ["Apple", "Banana", "Orange"]
print(len(fruits))Output
3This is helpful when checking whether a list contains data or controlling an indexed operation.
Key Takeaways
- A list is an ordered and mutable collection of items.
- Lists are created with square brackets.
- Items are accessed using indexes that start at zero.
- Lists support slicing to retrieve a range of items.
- Add items with append(), insert(), and extend().
- Remove items with remove(), pop(), and clear().
- Loops make it easy to process every item in a list.
- Methods such as sort(), reverse(), count(), and index() simplify common operations.
- List comprehensions provide a concise way to create new lists.
- Lists are among the most useful and widely used data structures in Python.