Principales métodos de las listas — 9:07 min

  • 9:07 min | Última modificación: Octubre 4, 2021

[1]:
#
# Método append
# =============================================================================
#
cubes_list = [1, 8, 27, 64, 125]

#
# Se agrega el cubo de 6 al final de la lista.
#
cubes_list.append(216)

#
# Se agrega el cubo de 7 al final de la lista.
#
cubes_list.append(7 ** 3)
cubes_list
[1]:
[1, 8, 27, 64, 125, 216, 343]
[2]:
#
# Método append usando el operador +
#
squares_list = [1, 4, 9, 16, 25]

#
# Concatenacion de listas usando el operador +
#
squares_list + [36, 49, 64, 81, 100]
[2]:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[3]:
#
# Método clear
# =============================================================================
#
list_to_clear = [1, 2, 3, 4, 5, 6]
list_to_clear.clear()
list_to_clear
[3]:
[]
[4]:
#
# Método pop
# =============================================================================
# Lista antes de pop()
squares_list
[4]:
[1, 4, 9, 16, 25]
[5]:
#
# Método pop
# =============================================================================
#
squares_list.pop()
[5]:
25
[6]:
#
# Contenido de squares despues de pop()
#
squares_list
[6]:
[1, 4, 9, 16]
[7]:
#
# Pop() indicando la posición
#
squares_list.pop(0)
squares_list
[7]:
[4, 9, 16]
[8]:
#
# Método extend
# =============================================================================
#
list_a = [0, 1, 2, 3, 4, 5]
list_b = [6, 7, 8, 9]

#
# Agrega lista_b al final de lista_a
#
list_a.extend(list_b)
list_a
[8]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9]:
#
# Método extend
#
lista_a = [0, 1, 2, 3, 4, 5, 6]
lista_b = [7, 8, 9]

#
# Se agrega lista_b al final de lista_a
#
lista_a.extend(lista_b)
lista_a
[9]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10]:
#
# Método insert
# =============================================================================
#
list_a.insert(0, "a")
list_a
[10]:
['a', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[11]:
#
# Método count
# =============================================================================
#
list_c = ["a", "b", "b", "c", "c", "c", "d", "d", "d", "d"]

#
# Cantidad de veces que aparece 'c'.
#
list_c.count("c")
[11]:
3
[12]:
#
# Para contar todos los elementos resulta más
# apropiado usar el objecto Counter del
# modulo collections
#
from collections import Counter

Counter(list_c)
[12]:
Counter({'a': 1, 'b': 2, 'c': 3, 'd': 4})
[13]:
#
# Mas común
#
Counter(list_c).most_common(2)
[13]:
[('d', 4), ('c', 3)]
[14]:
#
# Método index
# =============================================================================
#
list_c = ["a", "b", "b", "c", "c", "c", "d", "d", "d", "d"]
list_c.index("c")
[14]:
3
[15]:
#
# Elemento que no está en la lista
#
[ ]:
# error de elemento que no está en la lista
[16]:
#
# Método remove
# =============================================================================
#
letters_list = ["a", "b", "c", "d", "e", "f", "g"]
letters_list.remove("b")
letters_list
[16]:
['a', 'c', 'd', 'e', 'f', 'g']
[17]:
#
# Método reverse
# =============================================================================
#
letters_list = ["a", "b", "c", "d", "e", "f", "g"]

letters_list.reverse()
letters_list
[17]:
['g', 'f', 'e', 'd', 'c', 'b', 'a']
[18]:
#
# Método sort
# =============================================================================
#
letters_list = ["a", "b", "c", "d", "e", "f", "g"]
letters_list.sort(reverse=True)
letters_list
[18]:
['g', 'f', 'e', 'd', 'c', 'b', 'a']
[19]:
#
# Lista de tuplas
#
tuples_list = [(10, "b"), (8, "a"), (12, "d"), (9, "c")]
tuples_list
[19]:
[(10, 'b'), (8, 'a'), (12, 'd'), (9, 'c')]
[20]:
from operator import itemgetter

tuples_list.sort(key=itemgetter(0), reverse=True)
tuples_list
[20]:
[(12, 'd'), (10, 'b'), (9, 'c'), (8, 'a')]
[21]:
#
# Función sorted
# =============================================================================
#
letters_list = ["a", "b", "c", "d", "e", "f", "g"]
sorted(letters_list, reverse=True)
[21]:
['g', 'f', 'e', 'd', 'c', 'b', 'a']
[22]:
#
# La lista original queda intacta
#
letters_list
[22]:
['a', 'b', 'c', 'd', 'e', 'f', 'g']
[23]:
#
# Ordenamiento de listas complejas con sorted
#
tuples_list = [(10, "b"), (8, "a"), (12, "d"), (9, "c")]
sorted(tuples_list, key=itemgetter(1), reverse=False)
[23]:
[(8, 'a'), (10, 'b'), (9, 'c'), (12, 'd')]