• Inicio
  • Novedades
  • Academia SAP
  • FAQ
  • Blog
  • Contacto
S4PCADEMY_Logo
Twitter Linkedin Instagram
S4PCADEMY_Logo
Twitter Linkedin Instagram
FORMACIÓN PYTHON

Mejora tu eficiencia con Python List Comprehension: apend, ordena y obtén la longitud | Ejemplos

By s4pcademy 





Python List

What is a Python List?

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.

Examples of Python lists:

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:

  • list of integers = [1, 2, 3, 8, 33]
  • list of animals = [‘dog’, ‘cat’, ‘goat’]
  • list of names = [‘John’, ‘Travis’, ‘Sheila’]
  • list of floating numbers = [2.2, 4.5, 9.8, 10.4]

Examples of heterogeneous lists include:

  • [2, ‘cat’, 34.33, ‘Travis’]
  • [2.22, 33, ‘pen’]

Accessing values within lists:

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

Python List slicing

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).

…





Diferencias clave entre PHP y Python: ¿Cuál es mejor para tu proyecto?
Previo
Qué es el hacking: tipos de hackers y ciberdelitos (introducción al crimen cibernético)
Siguiente

Madrid

Calle Eloy Gonzalo, 27
Madrid, Madrid.
Código Postal 28010

México

Paseo de la Reforma 26
Colonia Juárez,  Cuauhtémoc
Ciudad de México 06600

Costa Rica

Real Cariari
Autopista General Cañas, 
San José, SJ 40104

Perú

Av. Jorge Basadre 349
San Isidro
Lima, LIM 15073

Twitter Linkedin Instagram
Copyright 2022 | All Right Reserved.