Tuples in Python

Tuple

A tuple can be thought of as a list which is immutable, or it cannot be changed or modified once created. So, lists are mutable while tuples, once created in memory, cannot be changed. The question then comes in mind is what is the application of this Tuple? Tuples basically are used to create: Records. For example, students record or telephone directory or some library records. The entries once done need not to be changed.  

How to create a Tuple?

Tuples are comma separated values like lists, but here we use normal parentheses unlike square parentheses in lists. But, yes, using parentheses is optional. It’s conventional though. Let’s see how it look like:

So, 

So, this is a tuple. Difference from the list is quite visible.

As I mentioned earlier, parenthesis is optional but it’s conventional. Without parentheses also, the code will run smoothly. What about the type?

It’s a tuple. Does tuple support indexing? 

Yes, it does. Now I want to add the original name of the actor in place of the screen name.

This is the difference between list and tuple. Look at the last line of the error. Tuple object doesn’t support item assignment. In simple words, you can’t modify tuple contents once created. Can we have a cross verification with a list? Of course, we can check if it were a list.

I created the same names and variables, but this time, in square brackets making it a list this time. And we can see that this list is mutable. Let’s check out what other differences there are in tuples.

We can concatenate. Let’s do some more concatenation using slicing and indexing as well.

OOPS!!! An error occurred. Why? Look at the last line. It says that, can only concatenate tuples (not “str”) to tuples. It means that we are concatenating tuples with a string. But here, iron_man is a tuple and “Robert Downey Jr.” has been written in normal brackets. Then, which of these is a string? Let’s check the type of “Robert Downey Jr.”

It’s showing it as a string. How’s that? It means that parenthesis is not the real difference between a list and tuple. The concept is that when using single element specially (or multiple elements also (not specifically)) in normal parenthesis use a comma also before closing the brackets.

See the comma inserted in the first line. Now the type is tuple. So, don’t get confused with brackets. The real concept lies with usage of commas along with brackets. Other than this, only an empty tuple is acceptable without a comma. 

Can we now concatenate? 

Now it’s working correctly. So, is this tuple modification? Technically, it is. But not direct methods by using append or extend. It is more like using the reference of earlier made tuples again. Talking about technicality, one more difference between list and tuple is that Tuple is a collection of Heterogeneous elements and List is a collection of Homogeneous elements. This is an ideal difference actually. It is expected that when we make a tuple we make it of heterogeneous elements and a list of homogeneous elements. But in reality, it is not possible. So, both list and tuple can have all types of data type elements possible within. But, an old stubborn professor can mind this real vs ideal check !!! 

Anyways, let’s do some more attribute checking.

That’s cool. So, tuples do not have a remove and pop attribute. But it does support del command, though item deletion is not possible. Obviously, we can delete a record. We can summarize the difference between list and tuple as follows.

  • List has append and extend attributes, tuple has not.
  • List has remove and pop attribute, tuple has not.
  • Both support indexing.
  • Both support searching using in operator.
  • Lists are mutable, tuples are immutable.
  • Ideally, tuples should be made up of heterogeneous and list should be made up of homogeneous elements. Practically, both support heterogeneous structure.

Tuple Assignment: Packing and Unpacking

One of the powerful features of tuple is tuple assignment. In interviews, it’s a direct question: what do you mean by tuple packing and unpacking? Let’s explore this very simple and very useful concept.

In tuple assignment, the values on the right hand side tuple of the assignment is assigned to the tuple on the left hand side. The major requirement is that the number of variables on the left must match the number of elements in the tuple. One can also say that, we are packing a number of items in one tuple on the left.

This is packing tuples. You can also observe that, this tuple is a collection of homogeneous elements.

By tuple unpacking, we mean that the values in a tuple on the right are ‘unpacked’ into the variables/names on the right:

This is quite impressive. The left tuple has been assigned with the values of the right tuple. Any constraints? Yes, the left and right tuple names should be the same. Here, the name is avengers.

If the name is different, Name Error will come as shown above. Application? Python provides a powerful feature of multiple assignments in a single line, such as swapping of two numbers, or assigning values to multi-variables in a single line. This we have seen in earlier chapters. Let’s explore it more. 

The value of 4 is assigned to a, and 5 is assigned to b. What if, there are more variables and fewer values.

It will give an error. But look at the statement. It says that there are not enough values to unpack. There are 3 variables but there are not enough values to assign to these variables. Let’s check what will be the output if there are more values and fewer variables?

It says that there are too many values to unpack and there are less variables. There are two ways to overcome this situation. One is to use the sign of underscore.

Here, underscore is acting like a variable and you can also print it. Another way is to use * as prefix with the variable.

If there are 3 variables and an asterisk sign is written with the 3rd variable, then, no matter how many values are there, 1st and 2nd values are assigned to 1st two variables, and all remaining values will be assigned to the third variable. Let’s check for one more example.

Here, the asterisk is with the first variable. Hence, the first 5 values are assigned to the first variable, and the next two values are assigned to the first two variables respectively.

All this is possible because of tuple packing and unpacking. Another important application of tuple packing and unpacking is returning multiple values in a function. We haven’t touched functions yet, so don’t stress your mind if you cannot understand the following code. The application is just to demonstrate the use of tuples in function. Here, our area of interest is line number 5, return (cir, area). This is tuple assignment. We will deal with this type of codes more when we’ll study functions in python later.

One can observe that we have passed two values in the return statement. This is possible using tuple packing features.

Before completing this topic of tuple, one last question is how we convert a list into a tuple and vice versa. For this, we use tuple() and list() commands respectively.

tuple() changes the list into a tuple. Similarly, we can change a tuple into a list using a list command.

This is all about tuples. We can move forward to another important feature, Dictionaries in python.

Design a site like this with WordPress.com
Get started