ELIF - Python


Lists are a fundamental data structure widely used in every major programming language. Python is not the exception. Lists in Python are heavily used and for that reason they're quite powerful.

Here are the fundamental things you need to know about lists in Python.

Check out our course: Introduction to Programming with Python

How to create a list

To create a list, just surround your elements between square brackets ([]).

    my_list = [1, 2, 3, 4]  # A list containing integers
    my_list = ['a', 'b', 'c', 'd']  # A list containing strings

Characteristics of Python Lists

Lists in Python are:

  • Ordered: Elements will conserve the order in which they were added.
  • Mutable: You can modify (mutate) a list (oposed to, for example, a tuple, which is immutable).
  • Iterable: You can iterate through a list. (more about this later).
  • Heterogeneous: As any other Python collection, you can store different types of objects inside a list.


    # Ordered
    my_list = ['z', 'b', 'r', 'd']
    print(my_list)  # ['z', 'b', 'r', 'd'] (Order is kept)

    # Mutable
    my_list = ['a', 'b', 'c']
    my_list.append('d')  # Add an element to the end
    my_list.insert(0, 'z')  # Add an element to the beginning

    # Heterogeneous
    my_list = [9, 'rmotr', date(2016, 1, 1)]  # Different object types in the same list

Operations with Lists

Once you have a list, you'll want to perform different operations with it. For example, counting elements, adding new elements, removing others, etc.

Python makes it easy to operate with your list. There are many different operations you can perform, we just show you a few examples for you to see how useful they are, but we encourage you to check the documentation for more.

my_list = ['a', 'b', 'c']

# Append an element (add at the end)

# Add an element in a certain position
my_list.insert(0, 'z')  # Beginning
my_list.insert(3, 'z')  # 4th position (index == 3)

# Count how many items the list has

# Remove all the elements from the list

# Reverse its items

# ... many more ...

Iterating a list

Iteration is key to understand how lists work. To properly understand lists you should be familiar with the for loop (control flow statement).

The good news is that it's simple to understand. Here are a few examples:

names = ['Mary', 'Tom', 'Rose']
for name in names:
# This will print the names, each in a new line:
# Mary
# Tom
# Rose

numbers = [0, 1, 2, 3, 4]
for number in numbers:
    # I can define whatever I want inside the **for loop body**
    double = number * 2
# This will print the numbers, doubled (one number per line):
# 0
# 2
# 4
# 6
# 8

As you can see in the previous example, a for loop is composed of a few things:

Python for loop explained

Note 1: for is a keyword, should always be used

Note 2: name in this case is a variable that we choose. We can choose whatever name we want for this variable (name, n, a_name). It'll reference each one of the elements in the list

Note 3: in is also a keyword. It precedes the list we'll iterate.

Note 4: names is the list itself. It's the list we want to iterate

Note 5: Finally, the for loop body. This is really important. We'll express here whatever we want to do with each one of the elements as we iterate through the list.

List indexing

Finally, you can retrieve individual elements from a list by specifying the position they have in the list. Indexes start from the number 0. So, the first element will have index 0, the second one will have index 1, the third one 3, etc... The last element of the list has index len(list) - 1 (if the list has 8 elements, the last one is 7 (8-1))

There are also negative indexes, the last element of the list also has the index -1, the second to last -2, etc.

names = ['Mary', 'Tom', 'Rose', 'Joe']
mary  = names[0]
tom   = names[1]
joe   = names[-1]
rose  = names[-2]

You can also get sublists from a given list. That'd be like a fraction of a list. A sublist specified by a range of indexes. For example, the elements between the second and the fifth position, the first three elements, etc.

To get a sublist you must specify two indexes separated by a colon sign. Example: my_list[INDEX-1:INDEX-2].

Important! The resulting sublist will NOT include the element at the last position specified. See examples below

names = ['Mary', 'Tom', 'Rose', 'Joe']
# First 3 names
# ['Mary', 'Tom', 'Rose']

# Just the first name
# ['Mary']

# Second and third
# [Tom', 'Rose']

# From the second element up to the end of the list
# ['Tom', 'Rose', 'Joe']

# The first index will be 0 by default, we can omit it:
# ['Mary', 'Tom', 'Rose']

# The last index will be the last element by default, we can also omit it
# ['Tom', 'Rose', 'Joe']