What a Python list is and why you need one

A list in Python is a container that holds multiple items in a single variable. Instead of creating ten separate variables to store ten names, you create one list and put all the names inside it. Lists are ordered — the first item stays first — and you can change them after you create them by adding, removing, or rearranging items.

Lists are one of the most common tools in Python because they let you work with groups of related data at once. You can loop through a list to do the same action to every item, search for a specific item, or sort everything alphabetically. Without lists, you would write far more code to accomplish the same task.

Key Takeaways

  • Create a list by putting items inside square brackets with commas between them: my_list = [1, 2, 3]
  • Access individual items by their position number, starting from 0: my_list[0] returns the first item.
  • Add items with the append() method or remove them with remove() and pop().
  • Loop through every item in a list using a for loop to perform the same action on each one.
  • Lists can hold any type of data — numbers, text, or even other lists mixed together.

Creating your first list with square brackets

The simplest way to create a list is to type the items you want inside square brackets, separated by commas. Here is a list of three numbers:

numbers = [10, 20, 30]

Here is a list of text items, called strings:

fruits = ["apple", "banana", "orange"]

You can also create an empty list and add items to it later:

empty_list = []

Python does not care what order you put items in, and you can mix different types in the same list. This list holds numbers, text, and a true/false value all together:

mixed = [5, "hello", True]

The list is now stored in the variable, and you can use that variable name anywhere in your code to refer to the entire list.

Accessing items by their position number

Each item in a list has a position number, called an index. The first item is always at index 0, the second item is at index 1, and so on. To get a specific item, write the variable name followed by the index number in square brackets.

If you have this list:

fruits = ["apple", "banana", "orange"]

Then fruits[0] gives you "apple", fruits[1] gives you "banana", and fruits[2] gives you "orange". If you try to access an index that does not exist — like fruits[5] when the list only has three items — Python will stop and show you an error.

You can also count backward from the end of the list using negative numbers. fruits[-1] always gives you the last item, fruits[-2] gives you the second-to-last item, and so on. This is useful when you do not know how many items are in the list.

Adding and removing items from your list

Lists are mutable, which means you can change them after you create them. The most common way to add an item is the append() method, which puts a new item at the end of the list:

fruits = ["apple", "banana"] fruits.append("orange") print(fruits)

This prints ["apple", "banana", "orange"]. The append() method adds only one item at a time, and it always goes to the end.

To remove an item, you have two main choices. The remove() method deletes the first item that matches what you specify:

fruits.remove("banana")

The pop() method removes an item by its index number and gives you that item back:

removed_item = fruits.pop(0)

This removes the item at index 0 and stores it in the variable removed_item. If you do not tell pop() which index to remove, it removes the last item by default.

Looping through a list to do something with each item

The real power of lists appears when you use a for loop to do the same action to every item. Here is a loop that prints each fruit:

fruits = ["apple", "banana", "orange"] for fruit in fruits:     print(fruit)

This prints each fruit on its own line. The variable fruit holds one item at a time as the loop goes through the list. You can name this variable anything you want — it is just a temporary holder for each item.

You can do more than print. Here is a loop that multiplies each number by 2:

numbers = [1, 2, 3, 4] for num in numbers:     print(num * 2)

This prints 2, 4, 6, 8. The loop runs once for each item in the list, and you can put any code inside the loop that you want to repeat.

Sorting, counting, and finding items in a list

Python gives you built-in methods to work with lists in common ways. The sort() method arranges items in order:

numbers = [3, 1, 4, 1, 5] numbers.sort() print(numbers)

This prints [1, 1, 3, 4, 5]. For text, sort() arranges items alphabetically. The len() function tells you how many items are in a list:

print(len(numbers))

This prints 5. The count() method tells you how many times a specific item appears:

print(numbers.count(1))

This prints 2 because the number 1 appears twice. The index() method finds the position of an item:

print(numbers.index(4))

This prints 2 because the number 4 is at index 2. If the item does not exist, Python shows an error.

Common mistakes when working with lists

The most common mistake is forgetting that indexing starts at 0, not 1. If you have three items and try to access my_list[3], you will get an error because there is no item at index 3 — the items are at indexes 0, 1, and 2.

Another mistake is confusing append() with other methods. append() adds a single item to the end. If you want to add multiple items at once, you need to call append() multiple times or use a different method like extend(). Also, append() changes the list in place — it does not return a new list, so do not try to assign the result back to your variable.

A third mistake is trying to modify a list while you are looping through it. If you remove items during a loop, the loop may skip items or behave unexpectedly. If you need to remove items, create a new list with only the items you want to keep, or loop through a copy of the list instead.

Frequently Asked Questions

Can I put different types of data in the same list?

Yes. A single list can hold numbers, text, true/false values, and even other lists all mixed together. Python does not require all items to be the same type. However, if you are looping through the list and doing something specific to each item, you need to make sure your code works with all the types you included.

What is the difference between remove() and pop()?

remove() deletes the first item that matches the value you give it — you say what you want gone, not where it is. pop() deletes an item by its position number and gives you that item back. Use remove() when you know the value, and pop() when you know the position.

How do I copy a list without changing the original?

Use the copy() method or slice notation. new_list = my_list.copy() creates a separate copy. If you just write new_list = my_list, both variables point to the same list, so changes to one affect the other.

Can I have a list inside a list?

Yes. A list can contain other lists as items. This is called a nested list: nested = [[1, 2], [3, 4], [5, 6]]. To access an item inside an inner list, use two index numbers: nested[0][1] gives you 2.

What happens if I try to access an index that does not exist?

Python stops running your code and shows an error message that says "IndexError: list index out of range". To avoid this, check the length of your list with len() before accessing an index, or use negative indexing to count from the end.