Lists in Python are collections that are both ordered and changeable. They are very similar to what other programming languages call arrays. Lists can contain as many items as you want and of any type.

Creating a List

Let's create our first list:

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals)
BASH
['elephant', 'tiger', 'lion']

The syntax is as straightforward as possible. Values are separated by commas, and they start and end with a bracket.

Accessing Items

The syntax for accessing an item is also pretty standard, simply provide the index of the item you want to access. Note that the first item has an index of 0:

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals[0]) print(animals[1]) print(animals[2])
BASH
elephant tiger lion

You can even use negative values to access it from the end:

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals[-1])
BASH
lion

Changing Items

Instead of accessing the items, you can choose to update the value at that index, like this:

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals) animals[1] = "bear" print(animals)
BASH
['elephant', 'tiger', 'lion'] ['elephant', 'bear', 'lion']

Appending Items

There is a built-in function to append items to the end of the list:

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals) animals.append("bear") print(animals)
BASH
['elephant', 'tiger', 'lion'] ['elephant', 'tiger', 'lion', 'bear']

Deleting Items

If there's an item in your list that you want to remove, you can delete it using the del function and passing in the index.

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals) del(animals[1]) print(animals)
BASH
['elephant', 'tiger', 'lion'] ['elephant', 'lion']

Alternatively, you can use the remove function which also removes the item given a value:

PYTHON
animals = ["elephant", "tiger", "lion"] print(animals) animals.remove("tiger") print(animals)
BASH
['elephant', 'tiger', 'lion'] ['elephant', 'lion']

Looping through a List

Looping through a list is a very common operation and Python luckily makes this easy to do:

PYTHON
animals = ["elephant", "tiger", "lion"] for animal in animals: print(animal)
BASH
elephant tiger lion

Checking if an Item Exists in List

Sometimes you might be working with huge lists or not even know what is inside the list, in the case of lists being returned from methods. You can very easily check if a list contains a value you want, like this:

PYTHON
animals = ["elephant", "tiger", "lion"] print("tiger" in animals) print("bird" in animals)
BASH
True False

List Length

For many different reasons, you'll want to know the number of items inside a list. You can get this value using the len() function:

PYTHON
animals = ["elephant", "tiger", "lion"] print(len(animals))
BASH
3

Combine Multiple Lists

Python has a simple syntax for combining lists, use the +.

PYTHON
na = ["USA", "Canada", "Mexico"] eu = ["UK", "France", "Germany"] countries = na + eu print(countries)
BASH
['USA', 'Canada', 'Mexico', 'UK', 'France', 'Germany']

Reverse a List

Reverse the order of the items in a list using the reverse function:

PYTHON
na = ["USA", "Canada", "Mexico"] na.reverse() print(na)
BASH
['Mexico', 'Canada', 'USA']

Sort a list

You can easily sort a list using the built-in sort function:

PYTHON
na = ["USA", "Canada", "Mexico"] na.sort() print(na)
BASH
['Canada', 'Mexico', 'USA']
Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.