Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a list in Python

Understanding Lists in Python

When you're just starting out with programming, you'll quickly come across the need to store and manipulate collections of items. In Python, one of the most versatile and commonly used data structures for this purpose is the list. Think of a list like a row of boxes, where each box can hold an item. These items can be anything: numbers, strings (text), or even other lists!

What Exactly is a List?

A list in Python is an ordered collection of items that can be changed after it's created (programmers call this 'mutable'). You can think of it as a shopping list where you can add, remove, or change the items as needed. The order of items in a Python list is exactly the order you put them in, and you can access these items by referring to their position in the list, which is called 'indexing'.

Creating a List

Let's start with how to create a list. It's as simple as putting different items between square brackets [] separated by commas. Here's an example:

my_first_list = [1, 2, 3, 4, 5]
print(my_first_list)

This list contains five items, all of which are numbers. When you run this code, Python will print out the list exactly as you've entered it.

Adding and Removing Items

Now, suppose you want to add an item to your list. You can do this using a method called append(). Here's how:

my_first_list.append(6)
print(my_first_list)

This code will add the number 6 to the end of the list. If you want to add an item at a specific position, you can use the insert() method:

my_first_list.insert(0, 0) # This adds the number 0 at the first position
print(my_first_list)

If you've changed your mind and want to remove an item, you can use the remove() method:

my_first_list.remove(3) # This will remove the number 3 from the list
print(my_first_list)

Accessing List Items

Accessing items in a list is straightforward. You use the index of the item (remember, indexing starts at 0, not 1):

print(my_first_list[0]) # This will print the first item in the list

If you try to access an index that doesn't exist, Python will give you an error. This helps you catch mistakes in your code.

Modifying Items in a List

Just like you can add or remove items, you can also change them. Let's change the first item in our list:

my_first_list[0] = 'a'
print(my_first_list)

Now the first item in the list is the letter 'a' instead of the number 0.

List Length and the len() Function

How do you find out how many items are in a list? You use the len() function, which stands for length:

print(len(my_first_list)) # This will print the number of items in the list

Looping Through a List

One of the most common things you'll want to do with a list is to look at each item one by one. This is called 'iterating', and you can do it with a for loop:

for item in my_first_list:
    print(item)

This loop will print each item in the list, one after the other.

Lists Can Hold Different Types

Remember when I said a list can hold anything? Let's mix it up:

mixed_list = [1, 'two', 3.0, [4, 'four'], True]
print(mixed_list)

This list has an integer, a string, a float (a number with a decimal), another list, and a boolean (True/False) value.

Slicing Lists

Sometimes you don't want the whole list, just a part of it. This is where 'slicing' comes in. You can slice a list using a colon : inside the square brackets:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:5]) # This will print items from index 2 up to, but not including, index 5

Nested Lists

A list can contain other lists. This is called a 'nested' list. It's like having a box that contains smaller boxes, each with their own items:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(nested_list[0]) # This will print the first inner list

Conclusion

Lists in Python are like Swiss Army knives for the programmer. They're incredibly versatile and will become a staple in your coding toolkit. You can put almost anything in them, change them on the fly, and use them to structure your data in a way that makes sense for your projects. As you continue to learn and experiment with lists, you'll discover more advanced features and techniques that will empower you to write more sophisticated and efficient programs. Remember, practice is key, so try creating and manipulating lists on your own to see what they can do. Before you know it, you'll be thinking in lists for all sorts of coding challenges!