"
This article is part of in the series
Published: Friday 16th June 2023

Python Lists: Quick Examples for Beginners

Python offers several ways to store data, including in lists, simplifying storing several values. 

Here's a good example to understand how lists are helpful: 

Let's say you're writing a program to store the marks of every student in a class of fifty. Instead of declaring fifty variables, you can declare one list variable to store the 50 values in.

Isn't this convenient? Python lists can make writing programs easier in many other ways, too. 

Let's see some quick examples so you can begin using Python lists in your programs.

Creating a Python List

Python creates a list out of the elements you enclose in square brackets. So, declaring "[]" will give you an empty list. 

If you declare [1,2,3], Python will create a list with those three numbers as its elements. 

To give the list a name, you simply have to assign it to a variable, like so:

listVariable = [2,5,7]

Note how the elements are separated with commas. You can also put string and Boolean values inside a list, like so:

listVariable = [4, "text", False, 50]

Accessing a Python List's Elements

You can use an element's index in the Python list to access its value. The first element in a Python list has the index 0, the second one has the index 1, and so on. 

Let's assume you have the following list:

listVariable = ["1", 2, "3", 4]

In this example, the string "1" will have index 0, the second element, 2, will have index 1, and so on. 

To access the third element in the list, you can write:

print(listVariable[2])

Using Negative Index Values in Python Lists

Negative index values are valid values in Python lists and behave differently than positive index values.

As you might be able to guess, the last element of a list has the index -1. The penultimate element has index -2, and so on.

So, to access the third value from the listVariable list in the previous example, you can write:

print(listVariable[-2])

Slicing Python Lists

Slicing a list in Python means accessing specific portions of the elements stored in the list. This is accomplished using the slicing operator, having the syntax: 

listName[a:b+1]

The syntax indicates that to access elements with index "a" through "b," you must use the operator by entering the "a" and "b+1" values.

Take the following list for instance:

listVariable = [1,2,3,4,5]

To see the three values in the middle of the list, you can run:

print(listVariable[1:4])

Let's say you want to access the elements starting from index 3 till the end of the list. You can run:

print(listVariable[3:])

To access the elements of the list starting from the beginning to index 3, you can run:

print(listVariable[:3])

To access all the values inside a list, you can run:

print(listVariable[:])

Changing Values Inside a Python List

The list datatype is a mutable datatype in Python. In simple words, you can change the values stored in a list after creating it. 

You can set a new value to a specific index number with the following syntax:

listName[indexValue] = newValue

Let's create a list and see this syntax in action:

listVariable = [2, 4, 6]
listVariable[2] = 8
print(listVariable)

This changes the third value in the list from 6 to 8.

But here's what's more interesting:

You can change several elements stored in a list in one go. Let's continue with the example list we created above and run: 

listVariable[0:2] = [3, 6]
print(listVariable)

The output of the list is [3,6,8].

Adding Values to an Existing Python List

The append() method is the easiest way to add a single value to the end of a list. Here it is in action:

listVariable = [3, 6, 8]
listVariable.append(10)
print(listVariable)

The output of this code is [3, 6, 8, 10].

You can also add values at specific positions inside a Python list using the insert() method. 

You must supply two arguments to use this method – the index where you want to insert the value and the value itself. 

Here's how it works:

listVariable = [3, 6, 8, 10]
listVariable.insert(3, 9)
print(listVariable)

The output of this code will be [3, 6, 8, 9, 10].

If you want to add multiple values to the end of the list, you can use the extend() method. The method accepts a list of values and inserts it at the end of a list. 

Here's the extend() method in action:

listVariable = [30, 50, 70]
listVariable.extend([90, 110])
print(listVariable)

As you can guess, the output here will be [30, 50, 70, 90, 110].

Another way to accomplish the same result is to use the + operator to concatenate the lists. Here's what this would look like:

listVariable1 = [30, 50, 70]
listVariable2 = [90, 110]
listVariable3 = listVariable1 + listVariable2
print(listVariable3)

The output of this code would be the same as the output in the code above.

Deleting Elements of a Python List 

You can use the del keyword to delete individual elements from a list:

listVariable = ["Red", 5, False]
del listVariable[2]
print(listVariable)

The code above deletes the third element in the list. 

You can also delete several values in a Python list with the del keyword in one shot. Here's how you can do this:

listVariable = ["Red", 5, False]
del listVariable[0:2]
print(listVariable)

This code deletes the list's first and second elements (index 0 and 1).

The del keyword also allows you to delete a list entirely. All you have to do is write the list's name after the keyword without any parenthesis. 

Have a look:

listVariable = ["Red", 5, False]
del listVariable
print(listVariable)

Run this code, and you will find that the output is an error since the list has been deleted, and the print function can't find it. 

Built-in Python Functions for Use with Lists

There are a handful of built-in Python functions you can use with lists. These include:

The len() Function

You can use this function to determine the size (more accurately, the length) of any list. All you have to do is pass the list's name to the method, like so:

listVariable = [3, 6, 9, 12, 15]
print(len(listVariable))

The output of this code is five. 

The max() Function

As the name suggests, this function returns the maximum value stored in a list. Like the len() function, you can pass the list's name to the max() function to get an output.

Let's see it in action:

listVariable = [4, 8, 12, 16, 22, 26]
print(max(listVariable))

The output of this code is 26. 

The min() Function

Serving the opposite function of the max() function, this function returns the smallest value stored in a list.

listVariable = [4, 8, 12, 16, 22, 26]
print(min(listVariable))

The output of this code is 4.

The sum() Function

The sum() function adds all the values stored in a list when you pass the list's name to it. Here's how it works: 

listVariable = [4, 8, 12, 16, 22, 26]
print(sum(listVariable))

The sorted() Function

You can pass any sequence data type, including Python lists, to the sorted() function to receive the same list but in sorted order. 

Here's an example of the function in use:

listVariable = [59, 35, 17]
print(sorted(listVariable))

The output of this code will be [17, 35, 59].

Besides lists, you can also use this function with collections (sets and dictionaries) and sequences such as tuples, ranges, and strings. 

The any() Function

This function checks whether any elements in a list have the value "True." 

If yes, it returns "True," else "False."

Let's have a look at the function in action:

listVariable = [31, 78, 66, True]
print(any(listVariable))

The output of this code is True. 

The all() Function

You can pass a list to the all() function to check whether all the elements in the list are True. 

Here's how it works:

listVariable = [5, 91, 15, True]
print(all(listVariable))

As you might have guessed, the output of this code is "True." If the list had a "False" value, the output would be "False."

Other Useful Python List Functions

The index() Function

When working with large lists, it doesn't make sense to figure out the index value of an element by counting the index values. 

It's easier to find the index values of the elements on which you want to perform operations with the index() function. 

Here's how you can pass a list and the value you want to find the index of to the index() function:

listVariable = ['Google', 'Fire', 53, True, 108, False, 'Amazon', 'Doll']
print(listVariable.index('Amazon'))

The output of this code is 6.

Interestingly, if the same value is repeated in a list, the index() function will return the index of the value that appears first in the list. 

listVariable = ['Google', 'Fire', 53, True, 108, False, 'Amazon', 'Doll', 53]
print(listVariable.index(53))

The code returns 2, but as you can see, the value 53 appears a second time at the end of the list. 

But what if you want to find the second occurrence of the value in the list?

The index() function accepts start and end index values. If you pass these values, the function will search for occurrences of the specified value within the range of supplied indexes. 

Here's what this looks like:

listVariable = ['Google', 'Fire', 53, True, 108, False, 'Amazon', 'Doll', 53, 99, 'Yahoo']
print(listVariable.index(53, 3, -1))

Remember the use of negative index values from earlier? 

In the code above, we're asking index() to search for 53 between the fourth value (since the function searches the start index value) and the final value of the list. 

The program outputs 8.

The count() Function

Let's say you have a list that has several repeated values. 

To find out how many times a value appears in a list, you can use the count() function like this:

listVariable = [53, 'Fire', 53, True, 108, 53, 'Amazon', 'Doll', 53, 99, 'Yahoo']
print(listVariable.count(53))

The program outputs 4.

The sort() Function

This function sorts values in increasing order by default:

listVariable = [53, 108, 13, 92, 10]
listVariable.sort()
print(listVariable)

The output is: 

[10, 13, 53, 92, 108]

If you pass reverse=True as an argument to this function, it will sort lists in decreasing order:

listVariable = [53, 108, 13, 92, 10]
listVariable.sort(reverse=True)
print(listVariable)

Here's the output:

[108, 92, 53, 13, 10]

The reverse() Function

Let's say you don't want to reorder the list in increasing order or otherwise but want to flip the order of the elements back to front. 

Here's where the reverse() function comes in handy:

listVariable = [53, 108, 13, 92, 10]
listVariable.reverse
print(listVariable)

The output is:

[53, 108, 13, 92, 10]

You're Ready to Use Python Lists!

Now that you've seen Python lists in action, you know how easy they are to use. Try creating lists and performing some operations. You'll quickly get the hang of using them.