Programs

List vs Tuple: Difference Between List and Tuple

In Python, list and tuple are a class of data structure that can store one or more objects or values. A list is used to store multiple items in one variable and can be created using square brackets. Similarly, tuples also can store multiple items in a single variable and can be declared using parentheses.

Although there are many differences between list and tuple, there are some similarities too, as follows:

  • The two data structures are both sequence data types that store collections of items.
  • Items of any data type can be stored in them.
  • Items can be accessed by their index.

List vs Tuple

The table below includes the basic difference between list and tuple in Python.

List Tuple
It is mutable It is immutable
The implication of iterations is time-consuming in the list. Implications of iterations are much faster in tuples.
Operations like insertion and deletion are better performed. Elements can be accessed better.
Consumes more memory. Consumes less memory.
Many built-in methods are available. Does not have many built-in methods.
Unexpected errors and changes can easily occur in lists.

 

Unexpected errors and changes rarely occur in tuples.

 

Learn more about free online python course for beginners here!

The following sections provide a detailed version of list vs tuple for better understanding.

Our learners also read: Excel online course free!

Difference in syntax

As mentioned in the introduction, the syntax for list and tuple are different. For example:

list_num = [10, 20, 30, 40]

tup_num = (10, 20, 30, 40)

Mutability

One of the most important differences between list and tuple is that list is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed.

So, some operations can work on lists, but not on tuples. For example, in data science, if a list already exists, particular elements of it can be reassigned. Along with this, the entire list can be reassigned. Elements and slices of elements can be deleted from the list.

On the other hand, particular elements on the tuple cannot be reassigned or deleted, but it is possible to slice it, and even reassign and delete the whole tuple. Because tuples are immutable, they cannot be copied.

Check out: Python vs Java 

Operations

Although there are many operations similar to both lists and tuples, lists have additional functionalities that are not available with tuples. These are insert and pop operations, and sorting and removing elements in the list.

Functions

Some of the Python functions can be applied to both data structures, such as len, max, min, any, sum, all, and sorted.

Must read: Data structures and algorithms free course!

Size

In Python, tuples are allocated large blocks of memory with lower overhead, since they are immutable; whereas for lists, small memory blocks are allocated. Between the two, tuples have smaller memory. This helps in making tuples faster than lists when there are a large number of elements.

Type of elements

Elements belonging to different data types, i.e., heterogeneous elements, are usually stored in tuples. While homogeneous elements, elements of the same data types, are usually stored in lists. But this is not a restriction for the data structures. Similar data type elements can be stored in tuples, and different data type elements can also be stored in lists.

Length

Lengths differ in the two data structures. Tuples have a fixed length, while lists have variable lengths. Thus, the size of created lists can be changed, but that is not the case for tuples.

Debugging

When it comes to debugging, in lists vs tuples, tuples are easier to debug for large projects due to its immutability. So, if there is a smaller project or a lesser amount of data, it is better to use lists. This is because lists can be changed, while tuples cannot, making tuples easier to track.

Nested lists and tuples

Tuples can be stored in lists, and likewise, lists can be stored inside tuples. In nested tuples, a tuple can hold more tuples. And in nested lists, a list can hold more lists.

Uses

It is important to understand that there are different cases where it is better to use one of these data structures, such as; using either one depends on the programmer, i.e., choosing one based on whether they want to change the data later or not.

Tuples can be used as equivalent to a dictionary without keys to store data. When tuples are stored within lists, it is easier to read data.

Read: More types of data structures in Python

Conclusion

This article helps in understanding the differences between lists and tuples. Even though both types are data structures in Python, it is important to be familiar with these differences when making a choice. The most important differences to keep in mind is that lists are mutable, and tuples are not, lists have variable sizes and tuples have fixed sizes. Lastly, operations in tuples can be executed faster.

If you are reading this article, most likely you have ambitions towards becoming a Python developer. If you’re interested to learn python & want to get your hands dirty on various tools and libraries, check out IIIT-B & upGrad’s Executive PG Programme in Data Science.

When is a Python list preferred for storing data?

Python list is considered to be the best data structure to store the data in the following scenarios: A list can be used to store various values with different data types and can be accessed just by their respective indices. When you need to perform mathematical operations over the elements, a list can be used since it allows you to mathematically operate the elements directly. Since a list can be resized, it can be used to store the data when you are not certain about the number of elements to be stored.

State the various operations of namedtuple.

The namedtuple in Python performs various operations. The following is a list of some of the most common operations performed by the namedtuple - Access by index, Access by key name, make(), _asadict(), using “**” (double star) operator, _fileds(), _replace(), etc.

What are the different ways of creating a list?

A Python list can be created in multiple ways that are mentioned here. Using for loops - A for loop is the most elemental way of creating a list. A list can be created using a for loop in three simple ways - Create an empty list, Iterate over all the elements that are to be inserted, Append each element in the list using the append() function. Using map(): The map() function in Python can be used alternatively to create a list. This function accepts two parameters - Function: The function to which the map passes each iterable and Iterable: The element or the iterable to be mapped. Using List comprehensions: This method is the most optimized of all three methods. While in the above methods an empty list has to be created first, list comprehensions allow you to insert all the elements in a list using a single line.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Let’s do it!
No, thanks.