A list in Python is a container that holds multiple items in a single variable
A list is one of Python's built-in data types that lets you store multiple values — numbers, text, or mixed types — in one place. You create a list by putting items inside square brackets, separated by commas. Python keeps track of the order, so you can retrieve items by their position.
Lists are useful because they let you work with groups of related data without creating a separate variable for each item. You can add items, remove them, change them, or loop through all of them with just a few lines of code.
Key Takeaways
- Create a list by typing items inside square brackets: my_list = [1, 2, 3] or fruits = ["apple", "banana"].
- Access individual items by their position number, starting from 0: fruits[0] returns "apple".
- Add items with .append(), remove them with .remove(), and check length with len().
- Loop through all items in a list using a for loop to perform the same action on each one.
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 are three examples:
numbers = [1, 2, 3, 4, 5] fruits = ["apple", "banana", "orange"] mixed = [1, "hello", 3.14, True]
The first list holds only numbers. The second holds only text (called strings in Python). The third mixes different types together — Python allows this. You can also create an empty list if you plan to add items later: empty_list = []
Accessing items by their position number
Once you have a list, you can pull out individual items by their position. Python counts positions starting from 0, not 1. So the first item is at position 0, the second is at position 1, and so on.
fruits = ["apple", "banana", "orange"] print(fruits[0]) # prints "apple" print(fruits[1]) # prints "banana" print(fruits[2]) # prints "orange"
You can also count backward from the end using negative numbers. fruits[-1] gives you the last item, fruits[-2] gives you the second-to-last, and so on. This is useful when you don't know how many items are in the list.
Adding and removing items from a list
Lists are not fixed in size. You can add items after you create the list using the .append() method, which adds one item to the end:
fruits = ["apple", "banana"] fruits.append("orange") print(fruits) # prints ["apple", "banana", "orange"]
To remove an item, use the .remove() method and tell it which item to take out. Python removes the first occurrence of that item:
fruits.remove("banana") print(fruits) # prints ["apple", "orange"]
If you want to remove an item by its position instead of its value, use .pop() with the position number: fruits.pop(0) removes the first item. You can also use .pop() without a position to remove the last item.
Looping through a list to work with each item
One of the most common reasons to use a list is to perform the same action on every item. A for loop lets you do this in just a few lines:
fruits = ["apple", "banana", "orange"] for fruit in fruits: print(fruit)
This code goes through each item in the list one at a time and prints it. The variable fruit (singular) holds the current item during each pass through the loop. You can name this variable anything you want — for item in fruits: or for x in fruits: would work the same way.
You can do more than just print. Inside the loop, you can perform calculations, check conditions, or modify the items. For example, this code prints each fruit with a message:
for fruit in fruits: print("I like " + fruit)
Checking the length and finding items in a list
The len() function tells you how many items are in a list. This is useful when you need to know the size or when you're looping and want to stop at a certain point:
fruits = ["apple", "banana", "orange"] print(len(fruits)) # prints 3
To check whether a specific item exists in a list, use the in keyword:
if "banana" in fruits: print("Banana is in the list")
You can also find the position of an item using the .index() method. It returns the position number of the first occurrence: position = fruits.index("banana") would store 1 in the variable position.
Frequently Asked Questions
What is the difference between a list and a tuple?
A tuple is similar to a list but uses parentheses instead of square brackets: (1, 2, 3). The key difference is that tuples cannot be changed after you create them — you cannot add, remove, or modify items. Lists are flexible; tuples are fixed. Use a list when your data might change, and a tuple when you want to protect your data from accidental changes.
Can I have a list inside another list?
Yes. A list can contain other lists: nested = [[1, 2], [3, 4], [5, 6]]. To access items in the inner lists, use two position numbers: nested[0][1] returns 2 (the second item in the first inner list). This is useful for organizing data in rows and columns.
What happens if I try to access a position that does not exist?
Python will show an error message saying the index is out of range. For example, if your list has 3 items and you try fruits[5], Python stops and tells you the position does not exist. Always check the length of your list or use a loop to avoid this problem.
How do I sort a list?
Use the .sort() method to arrange items in order: numbers = [3, 1, 4, 1, 5] then numbers.sort() changes the list to [1, 1, 3, 4, 5]. For text, .sort() arranges items alphabetically. Add reverse=True to sort in reverse order: numbers.sort(reverse=True).