Difference between list and tuple

Python is an interpreted, object-oriented, high-level programming language. Over the past few years, more and more people have started using python.

Python is an ideal teaching language for beginners. It is easy to learn the syntax of python, which in turn reduces the cost of maintenance of the program. With little investment of time and effort, great results can be achieved through programs written in python. The syntax is simple so that the code is readable and elementary.

Programs written in Python are easy to test and debug. Python allows the coder to focus on the logic of the program than on the syntax required for the programming language.

Python programming language is used in domains like data science, machine learning, scripting language for web applications, GUI based applications and many more.

Python may not be the fastest language but makes up with its advantages in versatility. Major API services and libraries support python services. Python also supports various modules and packers which allow code reuse and program modularity.

Python steadily advances with time and adds new features to our lives every day. Python is popularly used to build and solve complex algorithms.

In this article, we will discuss two data storing methods that contribute to the above-written features of python language- LIST and TUPLE.

Difference between list and tuple

List vs Tuple

The difference between list and tuple is compiled in the table as shown below.

List Tuple
Created using square brackets, [ ] Created using parentheses, ( )
Lists are mutable i.e. we can change/modify the values of a list Tuples are immutable i.e. we cannot change/modify the values of a tuple.
We can’t use a list as a key in a dictionary, as it is mutable We can use tuples as dictionary keys, as they are immutable
The list can be copied into other lists Tuples cannot be copied
The list has dynamic characteristics Tuples have static characteristics.
Small memory blocks are allocated to lists Memory is allotted as a large block with low overhead as they are immutable
Memory used by list is more than tuple Tuple will have a smaller memory usage compared to the list
Slower to compute in cases of a large number of elements than tuples Tuples are faster to compute in cases of a large number of elements
Lists can be used to store homogeneous as well as heterogeneous elements, i.e. they are elements that belong to the same data type or different data type. Tuples are used to store heterogeneous elements i.e. they belong to different data type
The size of a created list can be changed, it is of variable length We cannot change the size of an existing tuple, it is of fixed-length
Lists are used where you have a homogenous sequence of data of unknown length Tuples are used where the number of elements is known in advance because the position of the element is important.
The list has many built-in operations Tuples have lesser built-in operations
Errors are more likely to happen Errors hardly take place

What is list?

List is a common data storing method in python. In other basic programming languages, we use the array to save the elements of the same data type together.

In python, this feature was improvised and thus elements of different data types could also be stored. The syntax of the list is to use square brackets ( [ ] ) within which the elements are separated by a comma.

The first element of the list is represented by index 0. We can also use negative indices in a list, for example, the last element could be represented by -1.

The elements of a list can be modified i.e. they are mutable. Programmers can simply use insert(), del(), append(), etc. functions to add, delete or replace the elements in the list.

Two lists can be easily concatenated as well by using list1+list2 operation.

Through lists, it is possible to obtain a subsequence of elements. For example, print(list[2:5]) would print element from index 2 up to 5 which means the first index of the element is included but the last element isn’t.

List and tuples are the most commonly used data structures in python, followed by a dictionary. They all perform the same tasks. They are used to store items of various data types and store the collections of items as a sequence data type. Both maintain the order of their elements. Each item can be accessed via its index. Lists are very easy to implement in python.

What is tuple?

Tuples are similar to lists in some ways. Tuples, too, are used to store a sequence of elements. The syntax of a tuple is to use parentheses ( ( ) ) within which the elements are separated by a comma.

The elements of a tuple cannot be modified i.e. they are immutable.

A tuple can store elements of varying data types. The first element of the tuple is denoted by index 0.

Tuple can also have negative indices. We can find a subsequence of elements in tuple just like in lists.

Concatenation, length calculation, minimum, maximum and counting are all also supported by the tuple.

Tuples are immutable which signifies that once a tuple is created, elements cannot be inserted, deleted or modified in any way.

Author

Shriya Upasani
MIT World Peace University

References

1. https://stackabuse.com/lists-vs-tuples-in-python
2. https://www.geeksforgeeks.org/python-difference-between-list-and-tuple
3. https://www.programiz.com/python-programming/list-vs-tuples
4. https://www.python.org/doc/essays/blurb
5. https://nedbatchelder.com/blog/201608/lists_vs_tuples.html

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.