Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create list in Python

Getting Started with Lists in Python

Python is a high-level, interpreted, and object-oriented programming language that is easy to read and write. One of the most powerful features of Python is its ability to manipulate data structures, one of them being the list. In Python, a list is a type of data structure that can store multiple items in a single variable.

To put it simply, think of a list as a shopping list. You have a single sheet of paper (the list) where you can write down all the items you want to buy (the elements). This way, you can carry multiple items in a single sheet of paper rather than carrying different sheets for different items.

Creating a List in Python

Creating a list in Python is as simple as placing different comma-separated values between square brackets []. For example:

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

In the code above, fruits is a list that contains three items. Each item or value that is inside a list is called an element. Just like how each item in your shopping list is an element of the list.

You can also make a list of numbers:

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

Or a mix of both numbers and strings:

mixed = [1, "apple", 2, "banana"]

Accessing Elements in a List

To access elements in a list, you use the index number. The index number represents the position of an element in the list. It's like saying, 'the apple is the first item on my shopping list, so its index is 1.'

But in Python, indexing starts from 0, not 1. So, if apple is the first item, its index is 0. Here is how you can access elements:

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

This will output:


Modifying Elements in a List

Just like how you can cross off an item from your shopping list and replace it with another, you can also change the value of a specific item in a list by referring to its index number. Here's how:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "blackcurrant"

Now, the second item in the list (remember, we count from 0, not 1) is no longer "banana", but "blackcurrant".

Adding Elements to a List

Adding an item to your shopping list is easy, right? All you have to do is write the item at the end of the list. In Python, you can add an item to a list using the append() function:

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

Now, your fruits list will be ["apple", "banana", "cherry", "dragonfruit"].

Removing Elements from a List

To remove an item from a list, use the remove() function:

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

Now, your fruits list will be ["apple", "cherry"].


Lists in Python, like shopping lists in real life, are incredibly useful for keeping track of multiple items under a single variable. They are easy to create, access, modify, and manipulate. As a programmer, you'll find yourself using lists all the time, whether for processing data, building algorithms, or even creating games.

Just think of lists as your handy dandy notebook, ready to keep track of anything you need. The more you work with them, the more you'll understand their power and flexibility. Happy coding!