Lists

Lists

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:

	
    animals = ["elephant", "tiger", "lion"]
    print(animals)
	
	
    ['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:

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

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

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

Changing Items

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

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

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

Appending Items

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

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

    animals.append("bear")
    print(animals)
	
	
    ['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.

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

    del(animals[1])
    print(animals)
	
	
    ['elephant', 'tiger', 'lion']
    ['elephant', 'lion']
	

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

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

    animals.remove("tiger")
    print(animals)
	
	
    ['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:

	
    animals = ["elephant", "tiger", "lion"]
    for animal in animals:
        print(animal)
	
	
    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:

	
    animals = ["elephant", "tiger", "lion"]
    print("tiger" in animals)
    print("bird" in animals)
	
	
    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:

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

Combine Multiple Lists

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

	
    na = ["USA", "Canada", "Mexico"]
    eu = ["UK", "France", "Germany"]

    countries = na + eu

    print(countries)
	
	
    ['USA', 'Canada', 'Mexico', 'UK', 'France', 'Germany']
	

Reverse a List

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

	
    na = ["USA", "Canada", "Mexico"]

    na.reverse()

    print(na)
	
	
    ['Mexico', 'Canada', 'USA']
	

Sort a list

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

	
    na = ["USA", "Canada", "Mexico"]

    na.sort()

    print(na)
	
	
    ['Canada', 'Mexico', 'USA']