<jupyter_start><jupyter_text>Table of Contents<jupyter_code># 5 mins
# there is a nice ipython function called '%timeit'
# it lets you time things
%timeit [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] #list
%timeit (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25) #tuple

# using long tuples because python will casche tuples < 20
# iteration and conversion time for lists and tuples
# not a factor of 10, but a factor of 2
def rand(num_elements, num_rows):
    values = []
    for row in range(num_rows):
        row_value = list(range(num_elements))
        values.append(row_value)
    return values
test = rand(10, 4)
test
values = rand(10000, 4)

lst = [list(row) for row in values]
tup = (tuple(tuple(row) for row in values))

%timeit for row in lst: list(row)
%timeit for row in tup: tuple(row)tuple
lst = list(range(1000000))
tup = tuple(range(1000000))

%timeit list(lst)
%timeit tuple(tup)
%timeit for row in lst: a = row[0] + 1
%timeit for row in tup: a = row[0] + 1
# iteration and indexing time for lists and tuples
# almost the same
# reading rows out of a db, use a tuple
# where rows are the people
# databases are large, so this will be faster
# lists have a lot more methods
# so they are much more useful when building applications for people
# most of the time you will use a list
# for its flexability
def list_func():
    x = [1, 2, 3, 4, 5]
    b = x[0]

def tuple_func():
    x = (1, 2, 3, 4, 5)
    b = x[0]

import dis
print('list:')
dis.dis(list_func)

print('tuple:')
dis.dis(tuple_func)<jupyter_output>list:
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 LOAD_CONST               4 (4)
             12 LOAD_CONST               5 (5)
             15 BUILD_LIST               5
             18 STORE_FAST               0 (x)

  3          21 LOAD_FAST                0 (x)
             24 LOAD_CONST               6 (0)
             27 BINARY_SUBSCR
             28 STORE_FAST               1 (b)
             31 LOAD_CONST               0 (None)
             34 RETURN_VALUE
tuple:
  6           0 LOAD_CONST               7 ((1, 2, 3, 4, 5))
              3 STORE_FAST               0 (x)

  7           6 LOAD_FAST                0 (x)
              9 LOAD_CONST               6 (0)
             12 BINARY_SUBSCR
             13 STORE_FAST               1 (b)
             16 LOAD_CONST               0 (None)
             19 RETURN_VALUE
{'fraction_non_alphanumeric': 0.09427347097779509, 'fraction_numerical': 0.07557460070120764, 'mean_word_length': 1.7319148936170212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '88299', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1020, 'n_tokens_neox': 863, 'n_words': 339, 'path': 'notebooks/basics/08 - Tuple vs List Speed.ipynb', 'repo_name': 'chendaniely/2017-10-26-mckinsey', 'size': '2567'}