Python List with examples

Python list or array (depending of how you would like to call them) are one of the most functional and useful data types. They can be found anywhere and are probably one of the most used data types in this language.

Table of contents

In Python there are four groups of data types:

  • List
  • Tuple
  • Dictionary
  • Set

Today we are going to talk about the first group, list. It’s essential to understand each of theme to ensure you get the highest efficiency in your code. Selecting the wrong group can make your code work but it can be improved.

Python List

To keep it simple, we can sum up a list as a collection of ordered and changeable elements. We can compare a list to an array in other languages but way less restrictive. To create a list in python, all you have to type is []. Let’s take a look at an example:

mylist = ["copito", "system", "rabbit"]

print(mylist)

>>> ['copito', 'system', 'rabbit']

Python list has some important characteristics:

  • lists are ordered
  • list can contain any object
  • list can be nested as deep as you want
  • list elements can be accessed by an index
  • list are mutable


There is another, less used, way to create a list by using the constructor list() as shown bellow:

mylist = list(("copito", "system", "rabbit"))

print(mylist)

>>> ['copito', 'system', 'rabbit']

List are ordered

Compared to Dictionaries, list are ordered objects. Two list with the same objects but in different order are not equal.

a = [1, 2, 3, 4, 5]
b = [5, 4, 3, 2, 1]

a == b

>>> False

List can contain any object

As we said before, list can keep anything you like, from integers to strings or other lists. You can even mix them up all together.

a = [1, 2, 8, "copito", "2018", 2018, ["a", "b", 3], "system"]

When we say list can contain any object, we mean any object, like functions, classes or even modules

You can nest as many lists as you want

As we said before, a list can contain any object, that means we can add a list to a list.

a = ["a", "b", "c", ["d1", "d2", "d3"], "e", "f"]

Elements in a list can be accessed by an index

Once you create a list you may want to retrieve an specific element from it, to do that we must do as follows:

a = ["copito", "system", "rabbit"]

# a[0] to retrieve the first element
# a[1] for the second
print(a[0])

>>> "copito"

If you want to access the first element from a list you must use the index 0, for the second element 1 and so on.

Remember: Indices in python start at 0

Now, let’s take a look at our nested list example and try to return "d2":

a = ["a", "b", "c", ["d1", "d2", "d3"], "e", "f"]

# if we do a[0] we get the first element from the list
# if we do a[3] we will get the d's list
print(a[3])

>>> ["d1", "d2", "d3"]

# but we want just the 2nd element of that list

print(a[3][1])

>>> "d2"

If you try to access throw an index that is not in the list you will get an error.

There is no limit on how many list you want to nest, but make sure not to overflow you computer memory! Nesting list is also useful to create matrix, where the first index could be rows and the second columns.

Lists can be modified

This is easily explained with an example:

a = [1 , 1, 1]
a[1] = 5

print(a)

>>> [1, 5, 1]

What if we want to remove an item from a python list? Let’s say we have a list called basket where we save all our fruits, but then we ate all our apples so:

basket = ["oranges", "bananas", "apples", "watermelons"]

del basket[2]
print(basket)

>>> ["oranges", "bananas", "watermelons"]

There is also another way of doing the same:

basket = ["oranges", "bananas", "apples", "watermelons"]

basket.remove(2)
print(basket)

>>> ["oranges", "bananas", "watermelons"]

Remove method works like this: list_name.remove(index).

When it comes to add a new item to a list, or to say it the right way, to append an item to a list:

a = [1, 2, 3, 4]
a.append(5)

print(a)

>>> [1, 2, 3, 4, 5]

Adding a variable to a list

In short, if you add a variable to a list and that variable changes its value, it will NOT get updated inside the list. Let’s take a look at an example:

a = 5
mylist = ["copito", "system", a]
a += 5

print(mylist)

>>> ["copito", "system", 5]

Conclusion

In this tutorial we covered the basics of python lists. This data type collection will be really used in your code. For example a list called enemies can contain the health point of every target in your game. Make sure you clearly understand how to use lists, and if you need extra help feel free to leave us a comment bellow. See you in the next tutorial.


Leave a comment