In one of our previous articles, you saw how to use IF/ELIF and ELSE conditional statements in Python. In this article, you will see how to use Python lists.

Lists are used to store a collection of items. The items in a Python list can be of the same type or different types. Lists in Python are immutable, which means that you can add, update, and remove items from a Python list. In addition, you can clear all the items in a list as well.

Creating Lists in Python

There are several ways to creating lists in Python. To create a list, you have to add individual items inside square brackets. The items should be separated from each other via commas. Here is an example:

languages = ["Java", "PHP", "C++", "C#", "Python"]

The script above creates a list named languages, which contains 5 elements. Let’s check the type of the languages variable.

type(languages)

Output

list

Similarly, you can create a list using the list() function.

week_days = list()

An empty list week_days will be created as a result of the above script. To add an item to the list, you can use the append() method.

week_days.append("Sunday")

Displaying List Items

Items in Python lists are stored using a zero-based index. Consequently, the index of the first item in a Python list is 0, while the index of the last item is K-1, where K is the total number of items in a Python list.

To display an item from a list, you simply have to write the name of the list, followed by square brackets. Inside the square brackets, you need to pass the index of the item that you want to display. The following script prints the item at the first index of the languages list.

languages = ["Java", "PHP", "C++", "C#", "Python"]
print(languages[1])

Since the first index contains the string “PHP”, you will see that in the output.

Output

PHP

You can also traverse list items using a for loop:

languages = ["Java", "PHP", "C++", "C#", "Python"]
for lang in languages:
    print(lang)

Output

Java
PHP
C++
C#
Python

Adding Items to Lists in Python

To add a single item to a Python list, you can use the append() function. Similarly, to add multiple items, you can use the extend() method.

Example of adding a single item with append()

languages = ["Java", "PHP", "C++", "C#", "Python"]
languages.append("Joomla")
print(languages)

The script above adds a single item to the languages list. In the output, you will see the newly added item at the end of the list as shown below:

['Java', 'PHP', 'C++', 'C#', 'Python', 'Joomla']

Example of adding multiple items with extend()

langs = ["HTML", "CSS", "Laravel"]
languages = ["Java", "PHP", "C++", "C#", "Python"]
languages.extend(langs)
print(languages)

In the script above we define a list called langs, which contains three items. The items in the langs list are added to the languages list using the extend() method and the updated languages list is displayed.

Output

['Java', 'PHP', 'C++', 'C#', 'Python', 'HTML', 'CSS', 'Laravel']

Updating lists in Python

To update an existing Python list, all you have to do is write the name of the list to be updated followed by opening and closing square brackets. Inside the square brackets, pass the index value for the item that you want to update.

For instance, the following script updates the item at the 2nd index of the languages list.

languages = ["Java", "PHP", "C++", "C#", "Python"]
languages[2] = "Objective C"
print(languages)

In the above code, initially, the second index of the languages list contains the item “C++”. Next, the item at the second index is replaced by the string “Objective C”. Finally, the updated languages list is displayed. Here is the output:

['Java', 'PHP', 'Objective C', 'C#', 'Python']

Removing Items from Python Lists

To remove items from lists in Python, you can use the remove() function. The following script removes the item “PHP” from the languages list:

languages = ["Java", "PHP", "C++", "C#", "Python"]
languages.remove("PHP")
print(languages)

Output

['Java', 'C++', 'C#', 'Python']

You can see from the output that the item “PHP” has been removed.

To remove all items from a list in Python, you can use the clear() method. Here’s an example:

languages = ["Java", "PHP", "C++", "C#", "Python"]
languages.clear()
print(languages)

Output

[]

The above output shows that all the list items have been deleted once you call the clear() method on the list.

Some Useful List Functions

Following are some of the most commonly used list functions:

len() function

The len() function is used to count the number of items in a list.

integers = [1,5,7,3,9,4,1]
print(len(integers))

Output

7

max() function

The max() function finds the largest number in a list of integers. Look at the following example:

print(max(integers))

Output

9

min() Function

The min() function finds the smallest number in a list of integers. Look at the following example:

print(min(integers))

Output

1

sort() Function

The sort() function sorts the integers in a list, in the ascending order. Here is an example:

integers.sort()
print(integers)

Output

[1, 1, 3, 4, 5, 7, 9]

sum() Function

The sum() function adds all the integers in a Python list. Look at the following example:

print(sum(integers))

Output

30

Conclusion

This article explains how to use lists in Python. The article covers the process of creating, displaying, updating and removing list items. Finally, some of the most commonly used list functions have also been covered towards the end of the article.