Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Deciding-Whether-to-Use-Tuples

00:00 So when should you use tuples? You already know when you definitely shouldn’t use tuples. That’s when you need a container that can be changed after creation, but that’s pretty narrow.

00:10 So here are some general use cases for when tuples should be your preferred data type. Ensuring data integrity. Tuples are immutable. They can’t be modified after creation.

00:19 Knowing this fact, you can use tuples to ensure an extra layer of data stability because the values in the tuple will not be changed.

00:27 Reducing memory consumption. Because tuples are immutable, they have a reduced memory overhead when compared to other sequence data types. While it may not seem significant with small amounts of data, choosing tuples can be a significant benefit when working with large amounts of data or in situations where memory is limited, such as embedded systems. Improving performance.

00:48 Another benefit of tuples is that they are generally more efficient than lists in terms of creation, iteration, and element access. Tuples can significantly improve the performance of your programs, especially when dealing with large-scale data.

01:03 Let’s look at some more concrete use cases for tuples.

01:07 Associating two or more values, pairs, trios, etc. Tuples are the perfect choice for storing basic unchanging sequences. For example, in this line of code, we use a tuple to store an RGB color sequence 0, 2, 255.

01:24 If this were used as part of the configuration of a program, for instance, you wouldn’t want the values to change while the program is running. So that’s a great use of a tuple.

01:34 Tuples are also great for representing database records. You can use a tuple to represent a record received from a database. In this line of code, we use a tuple to store four pieces of data about a car probably retrieved from a database table: the make Toyota, the model Camry, the year 2020, and the color Blue.

01:57 In our last example, a really cool use case is to implement a multi-value key in a dictionary. As you now know, tuples are hashable. You can use this fact to create multi-value keys as seen below.

02:09 capital_cities is a dictionary. The first key is a tuple, where the first element is the string Ottawa, and the second element is a tuple containing latitude and longitude coordinates of the city as floating-point numbers. The corresponding value is Canada.

02:26 The second key is likewise a tuple with the string, Washington, DC and its coordinates. The value is USA. This is a great way to distinguish between cities of the same name and you couldn’t do it with a list.

02:38 But remember, for this to work each item in the tuple must be hashable.

02:43 Well, you’ve come a long way and all that’s left is to wrap up what you’ve learned. See you in the summary.

Become a Member to join the conversation.