A list in Python is a container that holds multiple items in a single variable

A list is one of the most basic ways to store data in Python. Instead of creating separate variables for each piece of information, you put related items into one list. Python keeps track of the order, lets you add or remove items, and lets you pull out any item by its position.

Lists are useful because they let you work with groups of data at once. You might have a list of names, a list of prices, or a list of true-or-false values. Once items are in a list, you can loop through them, count them, sort them, or search for a specific one.

Key Takeaways

  • Create a list by putting items inside square brackets, separated by commas: my_list = [1, 2, 3].
  • Access any item by its position number, starting from 0: my_list[0] returns the first item.
  • Add items with append(), remove them with remove(), and find the length with len().
  • Lists can hold different types of data at once — numbers, text, true-or-false values, or even other lists.

The basic syntax for creating an empty or populated list

The simplest way to create a list is to use square brackets. If you want an empty list, write just the brackets with nothing inside:

empty_list = []

If you want to put items in the list right away, write them inside the brackets, separated by commas:

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

You can also create a list with numbers, or mix different types together:

mixed = [1, "hello", 3.14, True]

Python does not care what order you put the types in. A list can hold strings (text in quotes), integers (whole numbers), floats (decimals), booleans (True or False), or even other lists inside it.

How to access items in a list by their position

Once you have a list, you can pull out any single item by writing the list name followed by square brackets with a number inside. That number is called the index. The first item is always at index 0, the second at index 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 of the list using negative numbers. Index -1 is the last item, -2 is the second-to-last, and so on:

print(fruits[-1]) # prints "orange" print(fruits[-2]) # prints "banana"

If you try to access an index that does not exist, Python will stop and show you an error. For example, if your list has 3 items, asking for index 5 will fail.

Adding, removing, and changing items in a 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:

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:

fruits.remove("banana") print(fruits) # prints ["apple", "orange"]

To change an item that already exists, use its index to replace it:

fruits[0] = "grape" print(fruits) # prints ["grape", "orange"]

You can also insert an item at a specific position using the insert() method. The first number is the index where you want it to go, and the second is the item itself:

fruits.insert(1, "banana") print(fruits) # prints ["grape", "banana", "orange"]

Finding the length and checking if an item is in a list

To count how many items are in a list, use the len() function:

fruits = ["apple", "banana", "orange"] print(len(fruits)) # prints 3

To check whether a specific item is in the list, use the in keyword:

if "banana" in fruits: print("Banana is in the list")

This is useful when you want to decide whether to do something based on what is already in your list. You can also use not in to check the opposite:

if "grape" not in fruits: print("Grape is not in the list")

Looping through a list to work with each item

One of the most powerful things you can do with a list is loop through it — that is, run the same code once for each item. Use a for loop to do this:

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

This code prints each fruit on its own line. The variable fruit holds one item at a time as Python goes through the list from start to finish.

You can also loop through a list using the index numbers if you need to know the position of each item:

for i in range(len(fruits)): print(i, fruits[i])

This prints the index number and the item at that index. The range() function creates a sequence of numbers from 0 up to (but not including) the length of the list.

Common list methods and what they do

Python lists come with built-in methods — actions you can perform on them. Here are the ones you will use most often:

sort() arranges items in order. For numbers, it goes from smallest to largest. For text, it goes alphabetically:

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

reverse() flips the list backward:

fruits = ["apple", "banana", "orange"] fruits.reverse() print(fruits) # prints ["orange", "banana", "apple"]

count() tells you how many times an item appears in the list:

numbers = [1, 2, 2, 3, 2] print(numbers.count(2)) # prints 3

index() tells you the position of the first time an item appears:

fruits = ["apple", "banana", "orange"] print(fruits.index("banana")) # prints 1

Frequently Asked Questions

Can a list hold different types of data at the same time?

Yes. A single list can contain strings, numbers, booleans, and even other lists. Python does not require all items to be the same type. For example, [1, "hello", True, 3.14] is a valid list.

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

Python will stop and show an error message that says "IndexError: list index out of range". To avoid this, check the length of your list first using len(), or use a loop instead of accessing by index.

How do I copy a list?

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

Can I put a list inside another list?

Yes. This is called a nested list. For example, matrix = [[1, 2, 3], [4, 5, 6]] is a list of lists. Access items using two index numbers: matrix[0][1] gets the second item from the first list.

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

append() always adds an item to the end of the list. insert() lets you choose exactly where the item goes. Use append() when order does not matter, and insert() when you need a specific position.