Encontrar el número de elementos en una lista en Python

Encontrar el número de elementos de una lista : Aquí, vamos a aprender a encontrar el número total de elementos en una lista en el lenguaje de programación Python?

Para encontrar el número total de elementos de una lista en Python – utilizamos el método len () , es un método incorporada, que acepta un argumento (esta lista) y devuelve el número total de elementos .

Sintaxis:

    len(list_object/list_elements)

Nota: Como argumento, un objeto de lista o una lista directa ([elemen1, elemento2, …]) se puede pasar.

Ejemplo 1:

# declaring lists
list1 = [10, 20, 30, 40, 50]
list2 = ["Hello", "IncludeHelp"]
# printing list and its length
print("list1: ", list1)
print("list2: ", list2)
print("len(list1): ", len(list1))
print("len(list2): ", len(list2))
# passing a direct list elements to the function
print("Here, elements are: ", len([10,20,30,40,50]))

salida

list1:  [10, 20, 30, 40, 50]
list2: ['Hello', 'IncludeHelp']
len(list1): 5
len(list2): 2
Here, elements are: 5

Ejemplo 2:

# declaring lists
list1 = [10, 20, 30, 40, 50]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]
# finding the lenght of the lists
# before appending the elements
print("Before appending...")
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))
# appending elements
list1.append(60)
list1.append(70)
list2.append(".com")
list3.append(30)
# finding the lenght of the lists
# after appending the elements
print() # prints new line
print("After appending...")
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))

salida

Before appending...
list1: [10, 20, 30, 40, 50]
list2: ['Hello', 'IncludeHelp']
list3: ['Hello', 10, 20, 'IncludeHelp']
Elements in list1: 5
Elements in list2: 2
Elements in list3: 4
After appending...
list1: [10, 20, 30, 40, 50, 60, 70]
list2: ['Hello', 'IncludeHelp', '.com']
list3: ['Hello', 10, 20, 'IncludeHelp', 30]
Elements in list1: 7
Elements in list2: 3
Elements in list3: 5

contando las apariciones de un elemento en la Lista de

para contar las apariciones de un elemento dado – utilizamos List.count método (), que acepta un argumento cuyas incidencias que se hayan encontrado y devuelve sus ocurrencias.

Ejemplo:

# declaring lists
list1 = [10, 20, 30, 40, 50, 10, 60, 10]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]
# printing the list and its elements
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))
# printing the occurrences of an element
print("occurrences of 10 in list1: ", list1.count(10))
print("occurrences of 60 in list1: ", list1.count(60))
print("occurrences of 70 in list1: ", list1.count(70))
print("occurrences of "Hello" in list2: ", list2.count("Hello"))
print("occurrences of "World" in list2: ", list2.count("World"))
print("occurrences of "IncludeHelp" in list3: ", list3.count("IncludeHelp"))

salida

list1:  [10, 20, 30, 40, 50, 10, 60, 10]
list2: ['Hello', 'IncludeHelp']
list3: ['Hello', 10, 20, 'IncludeHelp']
Elements in list1: 8
Elements in list2: 2
Elements in list3: 4
occurrences of 10 in list1: 3
occurrences of 60 in list1: 1
occurrences of 70 in list1: 0
occurrences of "Hello" in list2: 1
occurrences of "World" in list2: 0
occurrences of "IncludeHelp" in list3: 1


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *