Table of Contents
Python tuples and Python lists are two data typess used in Python Coding. Each of these data types is used for differetn purposes. They have some common properties and some differences. In this lesson, we will focus on Python Tuple vs List and we will learn the differences of these two python data types.
So, what are the differencesof Python Tuples and Lists? Let’s see each of them one by one.
You can also check Python Tuple To List Convertion
Tuples and lists used in python has different syntax. For python tuples, we use round paranthesis. But for python lists, we use square brackets. Between the members of both the tuples and lists, we use comma.
Let’s compare Python Tuple vs List and give an example to understand this difference. The first one is an example of a python tuple and the second one is an example of a python list.
As you can see above, python tuple has created with round paranthesis, python lists has created with square brackets.
The other diference of python tuples and python lists is about the creation. We can create a tuple in three ways like below:
The output of this codese will be same:
To create a python list, we can use two methods.
The outputs of these example will be same:
Mutability is one of the key differences if we compare Python Tuple vs List. Python tuples are not mutable, in other words they are immutable. We can not change a python tuple. They are defined and stay unchange in the code.
On the other hand, python lists are mutable. We can change, modify a python list in the code. We can add new members, we can remove memebrs etc.
Let’s do an example to show this difference. Below, for the first code, we will try to add a new member to a tuple.
The output of this code will be error. Because we can not modify python tuples.
Now, let’s try to add a new member to a python list and change it.
As you can see below, our list has changed and new member has added to the list.
The other differenxe of these two data type is speed. If we compare Python Tuple vs List, tuples are faster than lists. In a small codes we can not understand these speed but for long processes, this speed can be important.
So, if you need more speed, you can use python tuples. If speed is not important for your code, you can use python lists.
Like speed, memory usage is also important sometimes. If you have large code, this usage become more important. So, the memory usage of these two data types are different.
Tuples uses less memory than lists. And lists uses more memory than tuples.
In python, we can use tuple objects as keys for python dictionaries. But we can not use list objects as keys for dictionaries. Because, keys must be immutable and hashable.
Python tuples and python tuples uses some common and different methods. And python lists uses more methods than python tuples.
In this Python Tuple vs List lesson, we have list the differences of these two python data types. According to your need, you can use one of these data types or you can use them together in your code.