Categories
Python

Python Tuple: A Data Collection Type (Part 2 of 3)

Image for post
The partial image background was downloaded from Devostock images then edited by the author

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:

  1. A python tuple uses parentheses () not brackets []
  2. Items in a python tuple cannot be changed (immutable).
Python tuple vs list venn diagram for comparison
Python tuple vs list Venn diagram – Created by author

Additionally, if you would like to read more about python lists, you can check out my other article here.

In this article:

  1. Python Tuple Creation
  2. Accessing Elements
  3. Slicing
  4. Changing Elements

Creation Time!

Image for post
Photo by Akin Cakiner on Unsplash

First, you can make tuples in a variety of ways. Then when you compare them to lists, they are almost the default in python.

  1. Parentheses ()
  2. A trailing comma after an element ___,
  3. separating elements with commas on a line — element 1, element 2,…
  4. tuple()

For example:

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()
Various ways to make a python tuple

Accessing Elements in a Python Tuple

Image for post
Photo by Brienne Hong on Unsplash

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”)

tup[0]

OUTPUT:

screenshot displaying Python tuple indexing

Negative indexing also works well here too.

tup[-1]

OUTPUT:

screenshot displaying Python tuple negative indexing

Slicing

Now, if I would like to access the middle three items, I can slice the tuple.

tup[1:4]

OUTPUT:

screenshot displaying Python tuple indexing

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[2][0] = 30
tup2

OUTPUT:

screenshot displaying Python tuple indexing

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

OUTPUT:

screenshot displaying Python tuple repitition

The “+” operator joins two tuples together. This is known as concatenation.

(1, 2, 3) + (4, 5, 6)

OUTPUT:

screenshot displaying python tuple combination

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.

Summary:

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.

2 replies on “Python Tuple: A Data Collection Type (Part 2 of 3)”

Comments are closed.