Another kind of iterable in python is the tuple data type.
Tuples are created with parentheses ().
But can also be created without any parentheses! The use of just a comma implies a tuple.
Tuples
tuple_a = ('foo', 1)
tuple_a[0] # 'foo'
tuple_a[1] # 1for el in tuple_a:
print(el)
Elements in the tuple are also accessed via the index (like lists).
Tuples can be iterated over with a for loop, just like lists.
So what's the difference between a tuple and a list???
Tuples vs. lists
The biggest technical differences between a list and a tuple is that tuples have a fixed length and can't be mutated in-place.
However, lists can be used most places that a tuple is used, so the following rules can help you decide when to use a tuple and when to use a list:
LIST: Potentially many elements, unknown number of elements, relatively homogenous elements.
TUPLE: Few elements, fixed number of elements, completely heterogeneous elements.
Tuples vs. lists
The name comes from here: double, triple, quadruple, quintuple, sextuple, septuple, octuple.
Which gives a hint that they should be of fixed length! Because of this, we rarely iterate over them in a for loop like lists.