In this post, I will be going through 1 of the 3 built-in data types in python used to store collections of data: the Python Tuple.
What is a Python Tuple?
A python tuple is a collection of items that are ordered and immutable (cannot be changed).
A Python Tuple is identical to the list in all respects except for:
- A python tuple uses parentheses () not brackets 
- Items in a python tuple cannot be changed (immutable).
Additionally, if you would like to read more about python lists, you can check out my other article here.
In this article:
- Python Tuple Creation
- Accessing Elements
- Changing Elements
First, you can make tuples in a variety of ways. Then when you compare them to lists, they are almost the default in python.
- Parentheses ()
- A trailing comma after an element ___,
- separating elements with commas on a line — element 1, element 2,…
tup = ('Veery', 'Eastern Meadowlark', 10, 25, True)# trailing comma tup2 = 'Veery',tup = 'Veery', 'Eastern Meadowlark', 10, 25, True# convert from a list tuple(['Veery', 'Eastern Meadowlark', 10, 25, True])# blank tuple tuple()
Accessing Elements in a Python Tuple
Now, if you are familiar with accessing items within a list then you will have no trouble accessing items within a tuple. The method is exactly the same. Thus, you just need to put the index of the item you would like to access within brackets [ ]. Using the same example from above, you would like to access just the first element of your tuple (“Veery”)
Negative indexing also works well here too.
Now, if I would like to access the middle three items, I can slice the tuple.
Changing Elements in a Python Tuple
This is the main difference between lists and tuples. Tuples are immutable, meaning that you cannot change the items in the tuple. However, there are some methods to get around this, if you really have to. If the element is a mutable data type such as a list, then you can change items within that nested list.
tup2 = 'Veery', 'Eastern Meadowlark', [0, 24], [93, 11] print(tup2)tup2 = 30 tup2
Also, the + and * operators increase the number of times an element is repeated in a tuple. This will form new tuples that will have more elements within the tuple. However, notice that this doesn’t change the original elements of the tuple it only increases the number of items in the tuple.
tup = 'Tricolored Heron',tup * 3
The “+” operator joins two tuples together. This is known as concatenation.
(1, 2, 3) + (4, 5, 6)
All things considered, that’s it!! Tuples are a bit simpler than Lists since you do not have the ability to change individual elements. However, this is both a strength and a weakness depending on what you are looking to do with the items in the tuple.
In conclusion, use tuples when looking for a faster iteration through items that you do not need to change. Or you can use it as a key to a dictionary. Lastly, to make sure the data doesn’t get out of hand, use the tuple.
Still confused? Contact me to schedule a consultation to get control of your data.
Check out Part 1 of the Series on Lists here.
Check out Part 3 of the Series on Dictionaries here.
This article was originally posted on Medium.