A list is exactly what it sounds like, a container that contains different Python objects, which could be integers, words, values, etc. It is the equivalent of an array in other programming languages. It is represented by square brackets (and this is one of the attributes that differentiates it from tuples, which are separated by parentheses). It is also mutable, that is, it can be modified or updated; unlike tuples, which are immutable.
Python lists can be homogeneous, meaning that they can contain the same type of objects; or heterogeneous, including different types of objects.
Examples of homogeneous lists include:
Examples of heterogeneous lists include:
To access values within lists, the index of the objects inside the lists can be used. An index in Python lists refers to the position of an element within an ordered list. For example:
list = [3, 22, 30, 5.3, 20]
The first value in the list above, 3, has an index of 0
The second value, 22, has an index of 1
The third value, 30, has an index of 2 and so on.
To access each of the values from the list, you would use:
list[0] to access 3
list[1] to access 22
list[2] to access 30
list[3] to access 5.3
list[4] to access 20
The last member of a list can also be accessed by using the index -1.
For example, list[-1] = 20
List slicing is the method of splitting a subset of a list, and the indices of the list objects are also used for this. For example, using the same list example above; list[:] = [3, 22, 30, 5.3, 20] (all the members of the list); list[1:3] = [22, 30] (members of the list from index 1 to index 3, without the member at index 3); list[:4] = [3, 22, 30, 5.3] (members of the list from index 0 to index 4, without the member at index 4) list[2:-1] = [30, 5.3] (members of the list from index 2, which is the third element, to the second to the last element in the list, which is 5.3).
…
Calle Eloy Gonzalo, 27
Madrid, Madrid.
Código Postal 28010
Paseo de la Reforma 26
Colonia Juárez, Cuauhtémoc
Ciudad de México 06600
Real Cariari
Autopista General Cañas,
San José, SJ 40104
Av. Jorge Basadre 349
San Isidro
Lima, LIM 15073