leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Python List Basics Tutorial

Estimated reading: 5 minutes 83 views
Python List Basics Tutorial

We’ve learned about numbers and strings, two of the most common basic data types used in programs.¬†However, numbers and strings can also be inconvenient, so Python allows us to use lists to create data and group them together in a more efficient way.

For example, if you were asked to list a few of your best friends, you could create a list with the names of those friends in turn:

>>> bestFriends=["Jerry","Mark","Justin","Jonny"]
>>> bestFriends
['Jerry', 'Mark', 'Justin', 'Jonny']

That way, you can use a single list for all of those friends’ names without having to create 4 strings for that.

Getting to Know Python Lists

Take the example of listing a friend’s name. Suppose you want to use a program to keep track of your best friends. You can create a variable for each friend like this:

>>> bestFriend1="Jerry"
>>> bestFriend2="Mark"
>>> bestFriend3="Justin"
>>> bestFriend4="Jonny"
>>> bestFriend5="Tom"
>>> bestFriend6="Marry"
>> > bestFriend7="Jenny"
>>> bestFriend8="Daniel"
>>> bestFriend9="Tony"

However, it’s inconvenient to write this way, because now to keep track of all the friends’ names, these 9 different variables must be used. Imagine if 1000 animals were to be recorded? You need to create 1000 different variables, which is almost impossible work.

If you can put all 9 good friends together, obviously it will be easier. We can achieve this with lists.

Python create list

In Python, square brackets []are used to denote lists, and commas are used to separate the elements of the list. For example, you can create a list called bestFriends and save the names of your best friends in this list.

>>> bestFriends=["Jerry","Mark","Justin","Jonny","Tom","Marry","Jenny","Daniel","Tony"]

Tip: Sometimes, some lines of code are too long to fit on one line on the page, then the text of the code wraps to a new line. However, in the program entry, there is no line break, it is just because of the typesetting method. For example, in the example above, bestFriends comes right after >>>, not on two lines.If you ask Python to print the list, Python will print the full list of information, including the square brackets, as shown below.

>>> print(bestFriends)
['Jerry', 'Mark', 'Justin', 'Jonny', 'Tom', 'Marry', 'Jenny', 'Daniel', 'Tony']

Python access list elements

To access an element in a list, use square brackets followed by the desired element index. Still taking the bestFriends list as an example, suppose we want to access the 1st, 2nd, and 8th elements in the list. The implementation method is as follows:

>>> bestFriends=["Jerry","Mark","Justin","Jonny","Tom","Marry","Jenny","Daniel","Tony"]
>>> print(bestFriends)
[ 'Jerry', 'Mark', 'Justin', 'Jonny', 'Tom', 'Marry', 'Jenny', 'Daniel', 'Tony']
>>> bestFriends[0]
'Jerry'
>>> bestFriends [1]
'Mark'
>>> bestFriends[7]
'Daniel'


The element is the value stored in the list, and the index is the number corresponding to the position of the element in the list.

In Python, indices are counted from 0. Therefore, the index of the first list element is 0, not 1; the index of the second element in the list is 1, the index of the third element is 2, and so on. To access any element in the list, subtract 1 from its position as the index.

That’s why we ask the bestFriends list for an element at index 0 that returns “Jerry” (the 1st element in the list), while asking for an element at index 1 returns “Mark” (the 2nd element in the list).
Hint: In most programming languages, the index of a list is zero-based, which is related to the underlying implementation of list operations.The ability to access individual elements in a list is very useful. For example, if you want to introduce someone to one of your best friends, you don’t need to display the entire bestFriends list, but only the first element.

>>> bestFriends[0]
'Jerry'


Python provides a special method for accessing the last list element – by specifying the index as -1, you can tell Python to return the last list element. For example, bestFriends[-1] returns the last element “Tony”.

>>> bestFriends[-1]
'Tony'

This syntax is useful because we often need to access the last element without knowing the length of the list.

This notation also works for other negative indices, for example, index -2 returns the second-last list element, index-3 returns the third-last list element, and so on.

>>> bestFriends[-1]
'Tony'
>>> bestFriends[-2]
'Daniel'
>>> bestFriends[-3]
'Jenny'

Python modify list elements

We can use the index in square brackets to modify the elements in the list.

The syntax for modifying a list element is similar to that for accessing a list element. To modify a list element, specify the list name and the index of the element to be modified, and then specify the new value of the element.

Using the bestFriends list in the previous tutorial as an example, to replace the first element “Jerry” in the bestFriends list with “Christina”, the operation is as follows.

>>> bestFriends[0]="Christina"
>>> print(bestFriends)
['Christina', 'Mark', 'Justin', 'Jonny', 'Tom', 'Marry', 'Jenny', 'Daniel' , 'Tony']As you can see, the first element in the list has changed from "Jerry" to "Christina".
CONTENTS