Enchanted Code

Python 3 lists tutorial

5 minutes read

Intro

This tutorial will teach you about the lists data-structure in Python.

Requirements

  • Know basics of Python 3 if not read here
  • Knowledge of Computer Science data-structures will help

What are they?

Lists is a data-structure in Python that allows for storing multiple items in one variable. Unlike arrays found in many programming languages Python’s lists allow for multiple data-types.

Creation

Before we can use a list we will have to create one.

1
2
list_of_things = ["item1", 10, 1.4]
print(list_of_things)

In Python you create a list by using square brackets; then for each item you have a comma separating each element, as you can see I have three different data-types the first being a string then a integer and lastly a float. Then I print the list, in Python this will output it’s contents. The output should be: ["item1", 10, 1.4].

You can create a empty list using my_list = []

Accessing One Element

Now we have created a list and outputted all elements let’s access just one element. In Python we can do this through indexes, these start at zero. So if we want the first element we would have to use the following:

1
2
my_first_item = list_of_things[0]
print(my_first_item)

As you can see we can get a item from the list using square brackets at the end of the variable name with the index we want inside them. When we run the code we should get: item1.

Accessing Multiple Elements

Python can also access multiple elements at once using slicing.

1
2
my_items = list_of_things[0:2]
print(my_items)

As you can see from the code the colon allows a range to be retrieved. When this code is run it should output: ['item1', 10].

You also don’t have to specify a start or end index depending on what range needs to be selected. The code below will create the same result:

1
2
my_items = list_of_things[:2]
print(my_items)

You can also access all elements by using [:], although you can already do this by simply accessing the list by it’s variable name.

Modify Element

To modify a element in a list you have to first access the index to change then make it equal to another variable. This can be shown below:

1
2
list_of_things[1] = "something else"
print(list_of_things)

As you can see this code is changing the value at index 1 which will produce the following output: ["item1", "something else", 1.4].

Add Element(s)

You can add elements multiple ways. These being append, insert and extend.

Append

The most basic way of adding a new element to a list is through the “append” method, this will add a new element to the end of the list.

1
2
3
my_list = []
my_list.append("hello")
print(my_list)

As you can see when you “call” the function (which we will cover in another tutorial) and pass in a value/variable the list will be updated to have the element. The code will output: ["hello"].

Insert

The next way of adding to a list is using the “insert” function. This function allows you to insert a element at a certain index.

1
2
3
my_list = ["world"]
my_list.insert(0, "hello")
print(my_list)

From the code above you can see that the insert function will have the first parameter as the index to insert at. When the above code is run it will output ["hello", "world"].

Extend

The last way allows you to extend one list with another iterable, which can be a list.

1
2
3
4
my_list = ["hello"]
my_list_extra = ["world"]
my_list.extend(my_list_extra)
print(my_list)

The “.extend” is being passed a iterable, which in our case is a list. When the code is run it will output: ["hello", "world"].

Remove Element

Python also allows us to remove elements from a list. These being “pop”, “remove” and “del”.

Pop

Pop will allow you to remove an element from a list. This method will also allow return the element so we can store it in a variable.

1
2
3
4
my_list = ["hello", "world"]
my_var = my_list.pop(1)
print(my_var)
print(my_list)

When the code runs the list will only have “hello” and the “my_var” variable will have “world”.

Which you can see from the output below:

1
2
world
["hello"]

Remove

Remove will allow you to remove a element by value rather than index.

1
2
3
my_list = ["hello", "world"]
my_var = my_list.remove("world")
print(my_list)

As you can see you pass a value/variable into the remove method which will remove the element. When the code is run it will output: ["hello"].

Del

Another way is to use the “del” keyword.

1
2
3
my_list = ["hello", "world"]
del my_list[1]
print(my_list)

As you can see from the code you simply place the “del” keyword in-front of selecting a index from the list. When the code is run it will output: ["hello"].

Extra Functionality

Lists also have extra functionality for example you can return the length.

1
2
3
numbers = [1, 2, 3, 4]
length = len(numbers)
print(length)

When this code is run you will see it outputs 4. Which is the length of the list called “numbers”.

There are many more functions that can be very useful…

See Also

Buy Me A Coffee