Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to declare a list in Python

Getting Started with Lists in Python

When you start learning any programming language, one of the most fundamental concepts you'll come across is data structures. Today, we're going to delve into one of the most commonly used data structures in Python - Lists.

What is a List?

In Python, a list is a type of data structure that allows you to store multiple items in a single variable. You can think of it like a shopping list. On your shopping list, you might have bread, milk, eggs, etc. In Python, you could store these items in a list.

In terms of technical jargon, lists are also known as arrays in other programming languages, but in Python, they're always referred to as lists.

Creating a List

Creating a list in Python is straightforward. You only need to write your list items between square brackets [] and separate them with commas. Let's create a list of grocery items:

grocery_list = ['bread', 'milk', 'eggs']

In this example, grocery_list is our list, and it contains three items. You can print the list with the print function:

print(grocery_list)

When you run this code, Python will output:

['bread', 'milk', 'eggs']

Lists are Flexible

One of the great things about lists in Python is that they are very flexible. They can store different types of data in the same list. For example, you can have a list that contains strings, integers, and even other lists. Here is an example:

mixed_list = ['bread', 10, ['apple', 'banana']]

In this list, 'bread' is a string, 10 is an integer, and ['apple', 'banana'] is another list.

Accessing List Items

To access items in a list, you use the index. The index is like the position number of each item. The key thing to remember is that Python starts counting from 0, not 1. So the first item is at index 0, the second item is at index 1, and so on.

To access an item, you write the list name followed by the index in square brackets. Let's get the first item from our grocery list:

first_item = grocery_list[0]
print(first_item)

When you run this code, Python will print 'bread', which is the first item in our list.

Modifying a List

Lists in Python are mutable, which means you can change their content. To change an item, you use the index. Here's how you can change 'bread' to 'baguette' in our grocery list:

grocery_list[0] = 'baguette'
print(grocery_list)

Now, our list looks like this: ['baguette', 'milk', 'eggs'].

Adding Items to a List

To add items to a list, you use the append() method. Let's add 'butter' to our grocery list:

grocery_list.append('butter')
print(grocery_list)

Our list now includes 'butter': ['baguette', 'milk', 'eggs', 'butter'].

Removing Items from a List

To remove an item from a list, you can use the remove() method. Let's remove 'milk' from our list:

grocery_list.remove('milk')
print(grocery_list)

Now, our list looks like this: ['baguette', 'eggs', 'butter'].

Conclusion

Lists are like the Swiss Army knife of Python. They are a versatile tool that you can use to store, access, modify, and organize data. As you continue your journey in learning Python, you'll find that most of the problems you'll solve will involve lists in one way or another. So, next time when you're grocery shopping and you reach for your shopping list, remember - you're not just shopping, you're also practicing Python lists in real life!