What a list is and why you need one

A list in Python is a container that holds multiple pieces of data 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. Python keeps track of the order, so you can retrieve any name whenever you need it.

Lists are one of the most common tools in Python because real programs deal with collections of things — customer names, product prices, test scores, dates. Without lists, your code would become repetitive and hard to manage. A list lets you work with many items using the same few lines of code.

Key Takeaways

  • Create a list by typing a variable name, an equals sign, and items inside square brackets separated by commas: names = ["Alice", "Bob", "Carol"].
  • Access any item in the list by its position number in square brackets, starting from 0: names[0] returns "Alice".
  • Add items to a list with the append() method, which puts the new item at the end: names.append("David").
  • Loop through every item in a list using a for loop to perform the same action on each one without writing the action multiple times.
  • Lists can hold any type of data — text, numbers, or even other lists — and you can mix types in the same list.

Creating your first list with square brackets

The simplest way to create a list is to type a variable name, then an equals sign, then items inside square brackets with commas between them. Here is a list of three colors:

colors = ["red", "blue", "green"]

Each item is called an element. The square brackets tell Python "this is a list". The quotes around each color tell Python "this is text". If you are storing numbers instead of text, you do not need quotes:

scores = [85, 92, 78, 88]

You can also create an empty list and add items to it later. This is useful when you do not know what items you will have yet:

shopping_list = []

An empty list is still a list — it just has zero elements right now. You will add to it as you go.

Getting items out of a list by position

Once you have a list, you retrieve items from it using their position, also called an index. Python counts positions starting from 0, not 1. This confuses many people at first, but it is how Python works.

If you have colors = ["red", "blue", "green"], then:

  • colors[0] gives you "red" (the first item)
  • colors[1] gives you "blue" (the second item)
  • colors[2] gives you "green" (the third item)

If you try to access a position that does not exist, Python stops and shows an error. colors[5] will fail because there is no sixth item in the list. You can use negative numbers to count backward from the end: colors[-1] gives you the last item ("green"), and colors[-2] gives you the second-to-last item ("blue").

Adding and removing items from a list

The most common way to add an item is the append() method. It puts the new item at the end of the list:

shopping_list = ["milk", "eggs"]shopping_list.append("bread")Now shopping_list is ["milk", "eggs", "bread"]

If you want to insert an item at a specific position instead of the end, use the insert() method. You tell it the position and the item:

shopping_list.insert(1, "butter")

This puts "butter" at position 1, shifting everything else down. Now the list is ["milk", "butter", "eggs", "bread"].

To remove an item, use the remove() method with the item itself, or the pop() method with the position. shopping_list.remove("butter") removes the first "butter" it finds. shopping_list.pop(1) removes whatever is at position 1.

Looping through every item in a list

When you have a list of items and need to do something to each one, you use a for loop. This runs the same code once for each item, automatically:

colors = ["red", "blue", "green"]for color in colors:    print(color)

This prints each color on its own line. The word color (singular) is a temporary variable that holds whichever item the loop is currently working on. On the first run, color is "red". On the second run, it is "blue". On the third run, it is "green". Then the loop stops because there are no more items.

The indentation (the spaces before print(color)) matters in Python. Everything indented under the for line is part of the loop. If you write code without indentation after the loop, it runs only once, after the loop finishes.

You can do anything inside the loop, not just print. You could add numbers together, check if an item matches something, or build a new list from the items you find.

Working with different data types in lists

Lists can hold any type of data. You can store text, whole numbers, decimal numbers, or even other lists inside a list. You can also mix types in the same list:

mixed = ["Alice", 25, 3.14, True]

This list has text, an integer, a decimal number, and a boolean (True or False). Python keeps track of what type each item is, so when you retrieve an item, it remembers whether it is text or a number.

A list inside a list is called a nested list. This is useful for storing structured data like a table:

students = [["Alice", 85], ["Bob", 92], ["Carol", 78]]

To get Bob's score, you would use students[1][1] — the first [1] gets the second student, and the second [1] gets the second item in that student's data (the score).

Common list methods you will use

Python lists come with built-in methods that do useful things. You have already seen append(), insert(), remove(), and pop(). Here are a few more:

len(colors) tells you how many items are in the list. colors.sort() arranges items in alphabetical or numerical order. colors.reverse() flips the list backward. colors.count("red") tells you how many times "red" appears in the list. colors.clear() empties the entire list.

You can also check whether an item is in a list without looping through it: if "red" in colors: runs the code below it only if "red" is somewhere in the list. This is faster than looping when you just need a yes-or-no answer.

Frequently Asked Questions

Why does Python start counting from 0 instead of 1?

Many programming languages count from 0 because it matches how computers store data in memory. The first item is 0 positions away from the start. It feels unnatural at first, but you get used to it quickly. The important thing is to remember: position 0 is the first item, not the second.

Can I change an item that is already in a list?

Yes. You retrieve it by position and assign a new value: colors[0] = "yellow" changes the first item from "red" to "yellow". You can change any item this way without removing and re-adding it.

What is the difference between append() and insert()?

append() always puts the new item at the end of the list. insert() lets you choose the position. Use append() when order does not matter or you want the newest item at the end. Use insert() when you need the item in a specific spot.

Can a list contain another list?

Yes. Lists can hold any type of data, including other lists. This is called nesting and is useful for storing related groups of data, like a table where each row is a list. You access nested items by using multiple sets of square brackets.

What happens if I try to access a position that does not exist?

Python stops running your code and shows an error message saying the index is out of range. To avoid this, check the length of the list first with len(), or use a for loop, which automatically stops when it runs out of items.