Python

Tuple vs List Python

Python is an object-oriented language and takes everything as an object. List and Tuple are quite known objects used within Python. Both these objects are used to store different types of data. Both are similar to some extent and also different in some ways. Let’s get started with this article. After the successful login, you have to open its terminal shell application. Use the “Ctrl+Alt+T” shortcut at the Linux desktop to do so. After the successful opening of a shell, we have to install Python’s latest version in our system. Use the “apt” package installation command to install python3 on your system using the sudo rights. Add your Linux user password and hit the Enter key to continue. The Python version3 will be installed in no more than 1 minute.

After the Python installation, we have to create a Python file so that we can add some Python code for our examples. You need to utilize the “touch” keyword with the filename for a file to be created i.e., “test.py”. After the “test.py” Python file creation, open it in some editor like text, vim, and gnu editor. We have been utilizing the Linux “nano” instruction to open the “test.py” file in Ubuntu’s Gnu Nano editor. The empty file will be opened in 5 seconds.

Example 01: Syntax of List vs Tuple

We will be starting our first example by looking at the demonstration of both objects’ syntax. Within the Python file, we have added the python3 support extension i.e., #!/usr/bin/python3. After this, we have initialized a list name “ls” with 5 numerical values. All the values of the list have been initialized within the square brackets ‘[]’ separated from each other by commas. The print statement has been utilized to display the list “ls” on the shell. Now, we have initialized an object tuple “tp” with the same 4 numerical values as we initialized in a list “ls”.

All the values of a tuple “tp” are initialized within the simple brackets ‘()’ separated from each other by commas. The last print statement is utilized to display the tuple on the Ubuntu 20.04 console. This is done for the illustration of list syntax vs tuple syntax in the Python code. After that, we have tried to display the type of an object “ls” and object “tp” separately using the “type” function within the “print” clauses at two different lines. Now, save your test.py file on the system using the Ctrl+S shortcut and quit it via the Ctrl+X shortcut.

We are back in the shell terminal. It’s time to execute the python “test.py” file using the python3 command as displayed in the image. After the use of the execution command, we have been displayed the list and the tuple separately on different lines. The format for tuple and list is the same as we declared in the python code above i.e., [] for a list, and () for a tuple. The last two lines of output are showing the class type of object “ls” and “tp” i.e. “list” and “tuple”.

Example 02: List Functions for List vs Tuple

This example will be used to display all the possible functions for the list and tuple object separately. So, we have initialized a list ls with numerical values and used the print statement to display it on the shell. After this, we have used the “dir” function taking the “ls” list as an argument within the print clause. This will be used to display all the possible directories and functions of a list object. After that, we have been initializing a tuple “tp” with the same numerical values and printed it out on the terminal via the “print” clause. The last print clause is using the “dir” function taking the “tp” tuple as an argument to display all the possible directories and functions for a tuple object. Save this code and exit the file for sure.

We have executed the Python file “test.py” on the shell with the python3 keyword. The list of all possible directories for the object list and tuple have been displayed on our shell screen. You can see that the number of directories for the list is more than the number of tuples in the output shown below.

Example 03: Size of List vs Tuple

We will be taking a look at the size of the list as compared to the size of a tuple in Python code. We have been using the same code file and updated it a little. After initializing and printing the list and a tuple, we have used the other print statement that has been using the “__sizeof__” function to display the size of the list and tuple separately. This function has been called with list and tuple object by the “dot” sign and printed out at the shell.

On execution, the list and tuple are displayed separately along with their size as per the image below. You can see that the size of the list is greater than the size of the tuple i.e., 80 vs 64.

Example 04: Display of List vs Tuple

Just like the whole object, you can also display the chunk of an object on the shell. For instance, we have used the index of list and tuple separately within the print clauses to display the elements from a specific index. We have tried to get values of index 2, 3, and 4 from the list and tuple via “[2:5]” and displayed them on the shell. The index “5” is excluded here.

Running this file is showing full objects and sliced objects on the shell as expected.

We can also create a list of lists, a list of tuples, tuple of tuples, and tuple of lists as we did in the below-shown code. Let’s just run this code to see the output i.e., if it works or not.

After executing the shown-above code file, all the lists and tuples have been displayed the same as we have initialized in the above code file.

Example 05: Mutable List vs Immutable Tuple

Lists are said to be mutable because we can modify or update them. On the other hand, tuples are rigid and we can’t change them. That’s why tuples are said to be immutable. So, we have used the same list and tuple and displayed them using the print statements. After the display, we have used the assignment operator to replace the value at index 1 of the list and tuple with “16”. The updated list and tuple have been printed out again.

We have got the original list, updated list, and original tuple displayed. But, it throws an error when we tried to update the tuple i.e., TypeError.

Conclusion

Finally! We have done with the comparison of different properties of list and tuples with each other. We have tried to cover the syntax comparison, size comparison, display comparison, directory list comparison, and the mutability comparison of the list versus tuple. We have tried to make our illustrations as convenient as possible.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.