Tuples and Lists

list = []
tuple = ()

You can use the type() object to see what it is your working with.

print(type(list_or_tuple))

The key difference being:

  • Lists are mutable

  • Tuples are immutable

You can modify a list once declared but not a tuple.

  • Since lists are mutable they cannot be used in Dictionaries.

  • Since Tuples are mutable they cannot be copied.

If you try to copy a tuple it will simply return itself. If you run tuple(tuple_name), it will immediately return itself:

names = (Nicholas, Michelle, Alex)
copyNames = tuple(names)
print(names is copyNames)

Outputs:

True

The two are the same.

In contrast, list(list_name) requires copying of all data to a new list, for example:

names = [Nicholas, Michelle, Alex]
copyNames = list(names)
print(names is copyNames)

Outputs

False

Memory is allocated to tuples in larger blocks with a low overhead, because they are immutable. Tuples will use less memory than a dynamic list. A list has a variable size, a tuple is allocated a fixed size.


  • Dictionaries
    • Dictionaries are similar to arrays. They use keys and values to manage data (rather than indexes). Each value can be accessed by calling it’s key. A value can be any object (strings, ints, lists etc). Only immutable object can be written to a dictionary. Use tuples, not lists. (Ref: Tuples and Lists)