Tutorial: Creation and Manipulation of Lists in Python

A List is a value containing multiple values ​​in a sequence order. The term list refers to the list itself, not to the values ​​that are contained within it.

The List data type

A list takes values ​​like the following:

In this example we have values ​​of type string, which are separated by commas between each of them, and delimited by the symbols [], the values ​​within the list are called items.

Creating a list

To create a list in Python we just have to assign it to a variable, data separated by commas enclosed in square brackets. For example:

Here we create a list containing values ​​of type string, but the peculiarity of lists in Python is that it allows us to introduce different types of data in a single list, for example:

To print the values ​​of a list, use the method print()

Getting individual values ​​from a list

On the list New list we have several data of type string stored in a list, in order to get the individual values ​​contained in the list, we must access the position in which they are. The way it is done is to write the position number in brackets to the right side of the variable, as follows:

Lists in Python

We can also perform operations on the values ​​of the list without having to assign them to variables, for example:

Errors in lists

Traceback (most recent call last): File “<pyshell#9>”, line 1, in <module> New_list[10000] IndexError: list index out of range

It will return an error message, mentioning that the indicated position of the list does not exist and is out of range.
As positions of a list, integer values ​​must be entered, it is not acceptable to enter positions with decimal values ​​or floating point.

Traceback (most recent call last): File “<pyshell#13>”, line 1, in <module> New_list[1.2] TypeError: list indices must be integers, not float

Negative indices

To access a value from a list, we must enter the index in which it is located, but it is also possible to enter indices with negative values, and they are also accepted as positions in Python lists, the difference is that when placing a negative index, we will begin to obtain the values ​​of the list in reverse order, for example:

When entering negative indices we obtain the values ​​of the list from the end.

List in Python

Obtaining sublists with slices

We usually get a simple value from a list, but with slices we can get sets of values ​​from a list in a new list format. A slice is defined in brackets as an index, but in this case two integer values ​​are used separated by a comma.

  • New list[2] is a list with an index (a whole).
  • New list[1:4] is a list with a slice (two integers)


Get the length of a list with len()

To find out how many items are stored in a list, use is made of the function len(), where the value that is received is the list.

Changing values ​​of a list by means of the index

When we declare a list, we are assigning the values ​​that it will contain. Although it is probable that according to the execution of a program, it is necessary to change certain values ​​contained in a list, this can be done as follows:

Removing values ​​from a list

The declaration of the allows to eliminate values ​​of a list through its index.

Loop through a list with a for loop

To step through a list, we can do it using the for loop, iterating on list items:

Using the for loop, we can easily obtain all the values ​​of a list without having to indicate the index of each one
These are the correct ways to create and manipulate lists in Python, We saw why they are easy to manipulate in this language, in following posts later, We will see specific functions to perform different functions on Python lists.

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × one =