What a Python list is and how to make one

A list in Python is a container that holds multiple items in a single variable. You create one by putting items inside square brackets, separated by commas. The simplest list looks like this:

my_list = [1, 2, 3, 4, 5]

Python lists can hold any type of data — numbers, text (called strings), or even a mix of both. You can also create an empty list that you add items to later: empty_list = []. Lists keep items in the order you put them in, and you can change, add, or remove items whenever you need to.

Key Takeaways

  • Create a list by putting items inside square brackets separated by commas: [1, 2, 3] or ["apple", "banana", "cherry"].
  • Access individual items using their position number in square brackets, starting from 0: my_list[0] gets the first item.
  • Add items with the append() method, remove them with remove() or pop(), and change them by assigning a new value to their position.
  • Loop through all items in a list using a for loop to perform the same action on each one.
  • Lists are mutable, meaning you can modify them after creation, unlike some other Python data types.

Creating lists with different data types

You can put strings, integers, floats, or booleans into a single list. Here are some real examples:

fruits = ["apple", "banana", "cherry"] creates a list of text. numbers = [10, 20, 30, 40] creates a list of whole numbers. mixed = [1, "hello", 3.14, True] creates a list with four different types of data all together.

You can also create a list of lists, sometimes called a nested list. matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] creates a structure with three lists inside it. This is useful when you need to organize data in rows and columns.

Accessing items in a list

Each item in a list has a position number called an index. Python starts counting from 0, not 1. In the list ["apple", "banana", "cherry"], "apple" is at index 0, "banana" is at index 1, and "cherry" is at index 2.

To get an item, put its index number in square brackets after the list name: fruits[0] returns "apple". You can also count backward from the end using negative numbers: fruits[-1] returns the last item, fruits[-2] returns the second-to-last item, and so on.

If you try to access an index that does not exist, Python will stop and show an error. fruits[10] will fail if your list has fewer than 11 items.

Adding and removing items

The append() method adds a single item to the end of a list. fruits.append("orange") adds "orange" to the end of the fruits list. If you want to add an item at a specific position, use insert(): fruits.insert(1, "grape") puts "grape" at index 1 and shifts everything after it one position forward.

To remove an item, you have two main choices. The remove() method deletes the first item that matches a value: fruits.remove("banana") removes the first "banana" in the list. The pop() method removes an item by its index and returns that item: fruits.pop(0) removes and returns the item at index 0. If you do not give pop() an index, it removes the last item.

You can also delete an item using the del keyword: del fruits[1] removes the item at index 1. The difference is that del does not return the item, while pop() does.

Changing items and list length

To change an item that already exists, assign a new value to its index: fruits[0] = "blueberry" replaces "apple" with "blueberry" at position 0. You can change multiple items this way, one at a time.

To find out how many items are in a list, use the len() function: len(fruits) returns the number of items. This is useful when you need to loop through a list or check if it is empty. An empty list has a length of 0.

You can also replace a range of items at once using slice notation. fruits[1:3] = ["grape", "kiwi"] replaces items at index 1 and 2 with the new items. This is faster than changing items one by one when you need to swap out several in a row.

Looping through lists

A for loop lets you perform the same action on every item in a list. The basic structure is:

for fruit in fruits: print(fruit)

This loop goes through each item in the fruits list and prints it. The variable fruit holds the current item each time the loop runs. You can name this variable anything you want — for item in fruits: or for x in fruits: work the same way.

If you need the position number as well as the item, use the enumerate() function:

for index, fruit in enumerate(fruits): print(index, fruit)

This prints both the position and the item. enumerate() starts counting from 0 by default, but you can start from a different number if you need to.

Common list methods and operations

Python lists come with built-in methods that do useful things. The sort() method arranges items in order: numbers.sort() sorts a list of numbers from smallest to largest, and fruits.sort() sorts text alphabetically. The reverse() method flips the order: fruits.reverse() puts the last item first.

The count() method tells you how many times an item appears: fruits.count("apple") returns the number of times "apple" is in the list. The index() method finds the position of an item: fruits.index("banana") returns the index number where "banana" first appears.

You can also combine lists using the + operator: list1 + list2 creates a new list with all items from both. The extend() method adds all items from one list to another: fruits.extend(vegetables) adds every item from the vegetables list to the fruits list.

Frequently Asked Questions

Why does Python start counting from 0 instead of 1?

Python, like most programming languages, uses zero-based indexing because it relates to how computers store data in memory. The first item is at position 0, the second at position 1, and so on. This takes practice to get used to, but it becomes automatic once you write a few programs.

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

remove() deletes an item by its value — you tell it what item you want gone. pop() deletes an item by its position and gives you back that item so you can use it. Use remove() when you know what you want to delete, and pop() when you need to know what you deleted.

Can I have a list inside a list?

Yes. A list can contain other lists, called a nested list. matrix = [[1, 2], [3, 4]] creates a list with two lists inside it. To access an item in a nested list, use two index numbers: matrix[0][1] gets the second item from the first inner list.

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

Python stops and shows an IndexError message. For example, if your list has 5 items and you try to access index 10, Python will tell you the index is out of range. Always check your list length with len() if you are unsure whether an index exists.

How do I copy a list?

Use the copy() method or slice notation. new_list = fruits.copy() creates a separate copy. new_list = fruits[:] does the same thing. Without copying, new_list = fruits just creates another name for the same list, so changes to one affect the other.