Python List: A Comprehensive Guide for Beginners

In the previous post on Getting Started with Python, I briefly explained what virtual environments are and how to create one. Moreover, I also introduced the print() function that allows us to print a statement of our choice. In this article, we will proceed to understand Python list.

Python lists are powerful, flexible, and essential data structures that enable you to organize, store, and manipulate collections of elements with unparalleled ease. Whether you’re a coding novice taking your first steps or an experienced developer looking to deepen your understanding, this blog post will guide you through the fundamentals of Python lists, exploring their syntax, operations, and practical applications. In this post, we will look at:

[Note: At the end of this post, you will find an online editor where you can try out the codes yourself.]

python list

Creating a Python list

Lists are used to store different items in a single variable. You can create a list using square brackets and listing the items in them.

myList = ['vettel', 'hamilton', 'perez']

Here, I created a list with the words vettel, hamilton, and perez. Note that these items are of string data type. You can also store integers or float values as items.

Adding items to the list

Expanding the list is simple and is done using append() function. append() comes in handy when you are working with loops and require to store data that occurs in every iteration.

myList.append('max')
myList.append('goatifi')

Output:

[‘vettel’, ‘hamilton’, ‘perez’, ‘max’, ‘goatifi’]

Note that we added the items max and goatifi separately as only one item can be appended to the list at a time.

Slicing the list

It is important to understand that every item in the list is associated with some index. List items can be accessed directly using their corresponding indices. The table below illustrates the indices of the items of myList.

01234
vettelhamiltonperezmaxgoatifi
Positve indexing

Slicing is done by selecting the range of indices we want to keep. For example, we want to slice the list from the word hamilton till max.

myList = myList[1:4]

Output:

[‘hamilton’, ‘perez’, ‘max’]

When slicing a list, the first index represents the item you want to start your list with, and the second index indicates the end of the sliced list and is exclusive, which is why in our example, the sliced list contained items from the index 1 to 3.

The indexing technique we saw until now is called positive indexing (starting from the beginning). An alternative to this is negative indexing. As you might have guessed, negative indexing starts from the end and moves in reversed direction.

-5-4-3-2-1
vettelhamiltonperezmaxgoatifi
Negative indexing

Thus, an alternative way to slice myList to obtain the same result as above will be:

myList = myList[1:-1]

Again, the end index is -1 which is exclusive, hence our output will have the words learning, site, and python.

Learning programming could sometimes get quite challenging. In case you have encountered any issues or challenges till now in this tutorial, feel free to get in touch on Instagram and get your questions cleared: @machinelearningsite.

Indexing and modifying the list

Indices are not only useful for slicing but also for accessing individual items.

item = myList[3]
print(item)

Output:

max

Besides, accessing items through indices are useful when we want to alter the value of any item. The fact that lists are mutable makes it easier to modify the contents even after the list is created, unlike tuples. In our example, the 3rd index in the list holds the value ‘max’ and we want to change it to ‘verstappen’.

myList[3] = 'verstappen'
print(myList)

Output:

[‘vettel’, ‘hamilton’, ‘perez’, ‘verstappen’, ‘goatifi’]

Clearing the list

In older versions of Python, lists could be cleared using clear() attribute. However, this is no longer available, so we will use del, which is an in-built function in Python.

del myList[:]

The command above tells Python to delete the items of myList starting from the first till the last.

Summary

In this post, we grasped basic understanding of Python list. We created a list, sliced it, indexed it, modified it, and ultimately cleared all the items from it. Feel free to go ahead and try it yourself in the online editor below.

If you enjoyed this blog, get in touch on my social media where I post tips and information on Python programming and machine learning:

Stay updated with the latest posts and blogs by subscribing to my monthly newsletter:

This Post Has One Comment

Leave a Reply