“Liste vs Tuple” Réponses codées

Liste vs Tuple

# compare the size
import sys
my_list = [0, 1, 2, "hello", True]
my_tuple = (0, 1, 2, "hello", True)
print(sys.getsizeof(my_list), "bytes")
print(sys.getsizeof(my_tuple), "bytes")

# compare the execution time of a list vs. tuple creation statement
import timeit
print(timeit.timeit(stmt="[0, 1, 2, 3, 4, 5]", number=1000000))
print(timeit.timeit(stmt="(0, 1, 2, 3, 4, 5)", number=1000000))
Tough Tarsier

Tuples vs liste

# A tuple might contain data about a person (heterogeneous type mixture)
person_a = (name, age, occupation, address)

# A list might contain a list of people (homogeneous type mixture - all tuples!)
people = [person_a, person_b, person_c]
Drab Dolphin

Réponses similaires à “Liste vs Tuple”

Questions similaires à “Liste vs Tuple”

Plus de réponses similaires à “Liste vs Tuple” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code