A list in Python is a container that holds multiple items in a single variable
A list is one of the most useful tools in Python. It lets you store many pieces of data — numbers, words, or even other lists — all in one place. Instead of creating ten separate variables for ten names, you create one list and put all the names inside it. Python then lets you access, change, or loop through those items whenever you need them.
Lists are ordered, which means the position of each item matters. The first item is always at position 0, the second at position 1, and so on. This ordering makes it straightforward to find exactly what you want. Lists are also changeable — you can add items, remove items, or swap them out after the list is created.
Key Takeaways
- Create a list by putting items inside square brackets, separated by commas: my_list = [1, 2, 3, "apple"].
- Access any item by its position number in square brackets, starting from 0: my_list[0] returns the first item.
- Add items with the append() method or insert them at a specific position with insert().
- Remove items using remove() to delete by value or pop() to delete by position.
- Loop through a list with a for loop to work with each item one at a time.
Creating a list with square brackets and commas
The simplest way to create a list is to type the items you want inside square brackets, with commas between them. Here is a list of three numbers:
numbers = [10, 20, 30]
Here is a list of words:
fruits = ["apple", "banana", "orange"]
A list can hold different types of data at the same time. You can mix numbers and words in the same list:
mixed = [1, "hello", 3.14, True]
You can also create an empty list and add items to it later:
empty_list = []
Accessing items by their position number
Each item in a list has a position, called an index. The first item is always at index 0, not index 1. This is important to remember. If you have a list of fruits, the first fruit is at position 0:
fruits = ["apple", "banana", "orange"] print(fruits[0]) — this prints "apple" print(fruits[1]) — this prints "banana" print(fruits[2]) — this prints "orange"
You can also count backward from the end of the list using negative numbers. The last item is at index -1, the second-to-last is at -2, and so on:
print(fruits[-1]) — this prints "orange" print(fruits[-2]) — this prints "banana"
If you try to access a position that does not exist, Python will show you an error. For example, if your list has three items and you ask for item at position 5, the program will stop and tell you the index is out of range.
Adding items to a list
The append() method adds a new item to the end of a list. This is the most common way to add things:
fruits = ["apple", "banana"] fruits.append("orange") print(fruits) — now the list is ["apple", "banana", "orange"]
If you want to add an item at a specific position instead of at the end, use the insert() method. You tell it the position number and the item to insert:
fruits = ["apple", "orange"] fruits.insert(1, "banana") print(fruits) — now the list is ["apple", "banana", "orange"]
The insert() method shifts everything after that position one spot to the right. So if you insert at position 1, the old position 1 becomes position 2.
Removing items from a list
The remove() method deletes an item by its value. You tell Python what item you want gone, and it removes the first one it finds:
fruits = ["apple", "banana", "orange"] fruits.remove("banana") print(fruits) — now the list is ["apple", "orange"]
If you know the position of the item you want to delete, use pop() instead. It removes the item at that position and also tells you what was removed:
fruits = ["apple", "banana", "orange"] removed_item = fruits.pop(1) print(removed_item) — this prints "banana" print(fruits) — now the list is ["apple", "orange"]
If you use pop() without specifying a position, it removes and returns the last item in the list.
Looping through a list to work with each item
A for loop lets you do something with each item in a list, one at a time. This is useful when you want to print every item, add them all together, or check if something is in the list:
fruits = ["apple", "banana", "orange"] for fruit in fruits: print(fruit)
This loop prints each fruit on its own line. The variable fruit holds one item from the list during each pass through the loop. On the first pass, fruit is "apple". On the second pass, it is "banana". And so on.
You can also use a loop to add numbers together or build a new list based on the old one:
numbers = [1, 2, 3, 4, 5] total = 0 for num in numbers: total = total + num print(total) — this prints 15
Common list methods and what they do
Python lists come with built-in methods that make common tasks faster. The len() function tells you how many items are in a list:
fruits = ["apple", "banana", "orange"] print(len(fruits)) — this prints 3
The sort() method arranges items in order. For numbers, it sorts from smallest to largest. For words, it sorts alphabetically:
numbers = [3, 1, 4, 1, 5] numbers.sort() print(numbers) — now the list is [1, 1, 3, 4, 5]
The reverse() method flips the list backward. The last item becomes first, and the first becomes last:
fruits = ["apple", "banana", "orange"] fruits.reverse() print(fruits) — now the list is ["orange", "banana", "apple"]
The count() method tells you how many times a specific item appears in the list:
numbers = [1, 2, 2, 3, 2, 4] print(numbers.count(2)) — this prints 3
Frequently Asked Questions
Can a list hold different types of data at the same time?
Yes. A list can contain numbers, words, true/false values, and even other lists all mixed together. Python does not care what type each item is. You might have [1, "hello", 3.14, [2, 3]] and Python will accept it without complaint.
What happens if I try to access a position that does not exist?
Python stops your program and shows an error message saying the index is out of range. To avoid this, check the length of your list with len() before accessing a position, or use a for loop to go through the list safely.
How do I copy a list?
You can create a copy with new_list = old_list.copy() or new_list = list(old_list). This matters because if you just write new_list = old_list, both variables point to the same list, so changes to one affect the other.
Can I combine two lists together?
Yes, use the plus sign to join them: list1 = [1, 2], list2 = [3, 4], combined = list1 + list2 creates [1, 2, 3, 4]. You can also use a loop with append() to add items from one list to another.