## Lists vs Tuples in Python

### Introduction

Lists and tuples are two of the most commonly used data structures in Python, with dictionary being the third. Lists and tuples have many similarities. Some of them have been enlisted below:

• They are both sequence data types that store a collection of items
• They can store items of any data type
• And any item is accessible via its index.

So the question we're trying to answer here is, how are they different? And if there is no difference between the two, why should we have the two? Can't we have either lists or tuples?

In this article, we will show how lists and tuples differ from each other.

### Syntax Difference

In Python, lists and tuples are declared in different ways. A list is created using square brackets [] whereas the tuple is ceated using parenthesis (). Look at the following example:

tuple_names = ('Nicholas', 'Michelle', 'Alex')
list_names = ['Nicholas', 'Michelle', 'Alex']
print(tuple_names)
print(list_names)


Output:

('Nicholas', 'Michelle', 'Alex')
['Nicholas', 'Michelle', 'Alex']


We defined a tuple named tuple_names and a list named list_names. In the tuple definition, we used parenthesis () while in the list definition, we used square brackets [].

Python has the type() object that helps us know the type of an object that has been created. We can use it as follows:

print(type(tuple_names))
print(type(list_names))


Output:

<class 'tuple'>
<class 'list'>


### Mutable vs. Immutable

Lists are mutable while tuples are immutable, and this marks the KEY difference between the two. But what do we mean by this?

The answer is this: we can change/modify the values of a list but we cannot change/modify the values of a tuple.

Since lists are mutable, we can't use a list as a key in a dictionary. This is because only an immutable object can be used as a key in a dictionary. Thus, we can use tuples as dictionary keys if needed.

Let's take a look at an example that demonstrates the difference between lists and tuple in terms of immutability.

Let us create a list of differen names:

names = ["Nicholas", "Michelle", "Alex"]


Let us see what will happen if we attempt to change the first element of the list from "Nicholas" to "Samuel":

names[0] = "Samuel"


Note that the first element is at index 0.

Now, let us display the contents of the list:

>>> names


Output:

['Samuel', 'Michelle', 'Alex']


The output shows that the first item of the list was changed successfully!

What if we attempt to do the same with a tuple? Let's see:

First, create the tuple:

 names = ("Nicholas", "Michelle", "Alex")


Let us now attempt to change the first element of the tuple from "Nicholas" to "Samuel":

names[0] = "Samuel"


Output:

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
names[0] = "Samuel"
TypeError: 'tuple' object does not support item assignment


We got an error that a tuple object does not support item assignment. The reason is that a tuple object cannot be changed after it has been created.

### Reused vs. Copied

Tuples cannot be copied. The reason is that tuples are immutable. If you run tuple(tuple_name), it will immediately return itself. For example:

names = ('Nicholas', 'Michelle', 'Alex')
copyNames = tuple(names)
print(names is copyNames)


Output:

True


The two are the same.

In contrast, list(list_name) requires copying of all data to a new list. For example:

names = ['Nicholas', 'Michelle', 'Alex']
copyNames = list(names)
print(names is copyNames)


Output:

False


Next, let us discuss how the list and the tuple differ in terms of size.

### Size Difference

Python allocates memory to tuples in terms of larger blocks with a low overhead because they are immutable. On the other hand, for lists, Pythons allocates small memory blocks. At the end of it, the tuple will have a smaller memory compared to the list. This makes tuples a bit faster than lists when you have a large number of elements.

For example:

tuple_names = ('Nicholas', 'Michelle', 'Alex')
list_names = ['Nicholas', 'Michelle', 'Alex']
print(tuple_names.__sizeof__())
print(list_names.__sizeof__())


Output:

48
64


The above output shows that the list has a larger size than the tuple. The size shown is in terms of bytes.

### Homogeneous vs. Heterogeneous

Tuples are used to store heterogeneous elements, which are elements belonging to different data types. Lists, on the other hand, are used to store homogenous elements, which are elements that belong to the same type.

However, note that this is only a semantic difference. You can store elements of the same type in a tuple and elements of different types in a list. For example:

list_elements = ['Nicholas', 10, 'Alex']
tuple_elements = ('Nicholas', "Michelle", 'Alex')


The above code will run with no error despite the fact that the list has a mixture of strings and a number.

### Variable Length vs. Fixed Length

Tuples have a fixed length while lists have a variable length. This means we can change the size of a created list but we cannot change the size of an existing tuple. For example:

list_names = ['Nicholas', 'Michelle', 'Alex']
list_names.append("Mercy")
print(list_names)


Output:

['Nicholas', 'Michelle', 'Alex', 'Mercy']


The output shows that a fourth name has been added to the list. We have used the Python's append() function for this. We could have achieved the same via the insert() function as shown below:

list_names = ['Nicholas', 'Michelle', 'Alex']
list_names.insert(3, "Mercy")
print(list_names)


Output:

['Nicholas', 'Michelle', 'Alex', 'Mercy']


The output again shows that a fourth element has been added to the list.

A Python tuple doesn't provide us with a way to change its size.

### Conclusion

We can conclude that although both lists and tuples are data structures in Python, there are remarkable differences between the two, with the main difference being that lists are mutable while tuples are immutable. A list has a variable size while a tuple has a fixed size. Operations on tuples can be executed faster compared to operations on lists.

Kenya
I am a programmer by profession. I am highly interested in Python, Java, Data Science and Machine learning. If you need help in any of these, don't hesitate to contact me: [email protected]