Basic Python Data Structures: Tuples

Rafay Syed
3 min readSep 11, 2019

In the first post of the Basic Python Data Structures series, I talked about Lists and how they can be used to store many values rather than having to create separate variables for each value. You can check that out here. This post will talk about the Tuple data structure and how it is used to store values.

Tuple

What kind of data structure is a tuple? A tuple is very similar to a list except that the values are stored in between parentheses (()) rather than brackets ([]) as we have seen in lists. An example of a tuple is shown below.

The values inside the tuple are in between parentheses. Now here is where a tuple differentiates from a list. Tuples are immutable data structures, meaning that you cannot modify the variable such as adding or removing elements. A tuple is fixed and cannot change. Now if you want to add an element to the “marvel_heroes” variable, you can do something like this, but just know that you are not modifying the existing variable. Instead, you are creating ANOTHER “marvel_heroes” variable that will only have the new element that you want to add as well as including the existing values from the previous “marvel_heroes” variable.

Let’s talk about the syntax of the variable above. What happens is that the new “marvel_heroes” variable will contain all the values from the previous marvel_heroes variable and also have the variable “Ant Man” added to it. When you add a value to a tuple, you must contain a comma that way Python knows that you are adding a value to a tuple. There is no append or remove method because you cannot modify an existing tuple variable. In comparison to a list, when you append or remove an element, you are modifying the existing list. But in a tuple, if you want to add a value to the same data, you would need to create another tuple even if it is the same name and follow the syntax shown above.

When you know that a data structure has the possibility of being modified, use a list rather than a tuple. For the “marvel_heroes” variable, it is better to use a list since there can be new members that can be added into the Marvel Universe (yes I am a fan 😛). Use a tuple only when you know that the data structure will always be fixed and never have to be modified.

Takeaway Points:

  1. Tuples are immutable; they cannot be modified
  2. You cannot add a value to an existing tuple, but you can create a new variable that can contain the same values from an existing tuple and then add a value in parentheses and adding a comma after that value.
  3. Use tuples only when something cannot be modified. It is also bad practice to create new tuples each time you want to add a new value with the existing values. This can waste space since each variable takes up some space in the RAM. With a list, you are only modifying the existing variable and not having to create a new one.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rafay Syed
Rafay Syed

Written by Rafay Syed

Software Engineer at Salesforce and Lifelong Student

No responses yet

Write a response