Python Lists

In this tutorial we will learn about Python Lists; how to create a list, access items, remove items, delete a list and so on.

In Python, Lists are constructed using square brackets [] and each item in the list is separated by a comma ,.

Python lists can contain multiple different types of objects, so they don’t all need to be strings, or integers. For example, we can have a list containing mixed types:

mixedTypesList = ['a', True, 1, 1.0]

How to Create a List

colorsList = ["red", "green", "blue"]
print(colorsList)

Output:

['red', 'green', 'blue']

How to Access Items of a List

Remember: the first item in the list is at index 0.

colorsList = ["red", "green", "blue"]
print(colorsList[2])

Output:

blue

Accessing a Range of Items (Slicing)

We can specify a range of items from a list by specifying the starting index and the end index. We use the : operator.

Note: In the following example, the output is from index 1 (included) to index 4 (excluded)

colorsList = ["red", "green", "blue", "orange", "yellow", "white"]
print(colorsList[1:4])

Output:

['green', 'blue', 'orange']

Negative Indexing

We can access the items on the list from the end by specifying a negative index value. For example -1 means the last item and -2 means the second last item.

colorsList = ["red", "green", "blue", "orange"]
print(colorsList[-1])

Output:

orange

Change The Value of an Item

colorsList = ["red", "green", "blue", "orange"]
colorsList[3] = "yellow"
print(colorsList)

Output:

['red', 'green', 'blue', 'yellow']

How to Loop Through a List

We can loop through a list using the for loop.

colorsList = ["red", "green", "blue", "orange"]
for i in colorsList:
    print(i)

Output:

red
green
blue
orange

How to Add Items to a List

There are two methods to add items to a List in Python, append() and insert()

The append() method adds items to the end of the list:

colorsList = ["red", "green", "blue", "orange"]
colorsList.append("yellow")
print(colorsList)

Output:

['red', 'green', 'blue', 'orange', 'yellow']

The insert() method adds item at a specified index:

colorsList = ["red", "green", "blue", "orange"]
colorsList.insert(2, "yellow")
print(colorsList)

Output:

['red', 'green', 'yellow', 'blue', 'orange']

How to Remove Items From a List

You can remove items from a list using several items:

remove() removes a specified item

colorsList = ["red", "green", "blue", "orange"]
colorsList.remove("orange")
print(colorsList)

Output:

['red', 'green', 'blue']

pop() removes an item at a specified index or removes the last item if no index supplied

colorsList = ["red", "green", "blue", "orange"]
colorsList.pop(1)
print(colorsList)

Output:

['red', 'blue', 'orange']
colorsList = ["red", "green", "blue", "orange"]
colorsList.pop()
print(colorsList)

Output:

['red', 'grenn', 'blue']

del() removes an item at a specified index or removes the whole list

colorsList = ["red", "green", "blue", "orange"]
del colorList[1]
print(colorsList)

Output:

['red', 'blue', 'orange']
colorsList = ["red", "green", "blue", "orange"]
del colorList
print(colorsList)

Output:

Traceback (most recent call last):
  File "pythonList.py", line 30, in <module>
    print(colorsList)
NameError: name 'colorsList' is not defined

clear() empties the list

colorsList = ["red", "green", "blue", "orange"]
colorList.clear()
print(colorsList)

Output:

[]

How to Get the Length of a List

You can get the list length by calling the len() function, e.g.:

colorsList = ["red", "green", "blue", "orange"]
print(len(colorsList))

Output:

4

Count Number of Specified Items

We can use the count() function on the list to get the number of occurances of a specified item in the list. For example:

colorsList = ["red", "green", "red", "orange"]
print(colorsList.count("red"))

Output:

2

How to Sort the Items of a List

In this case, the sort() function sorts the list alphabetically.

colorsList = ["red", "green", "blue", "orange"]
colorsList.sort()
print(colorsList)

Output:

['blue', 'green', 'orange', 'red']

Sort in Reverse Order

colorsList = ["red", "green", "blue", "orange"]
colorsList.sort(reverse=True)
print(colorsList)

Output:

['red', 'orange', 'green', 'blue']

How to Reverse the Items of a List

We can use, the reverse() function to reverse the list, e.g.:

colorsList = ["red", "green", "blue", "orange"]
colorsList.reverse()
print(colorsList)

Output:

['orange', 'blue', 'green', 'red']

How to Copy a List to Another List

We can use the copy() function to copy contents of a list to another list.

colorsList = ["red", "green", "blue", "orange"]
newList = colorsList.copy()
print(newList)

Output:

['red', 'green', 'blue', 'orange']

How to Join Two Lists Together

The easiest way to join two lists together is to use the + operator. For example:

colorsList = ["red", "green", "blue", "orange"]
numbersList = [1, 2, 3, 4]

numbersAndColors = colorsList + numbersList
print(numbersAndColors)

Output:

['red', 'green', 'blue', 'orange', 1, 2, 3, 4]