Difference between List and tuple in Python w3schools

1. Syntax difference

Syntax of list and tuple is slightly different. Lists are surrounded by square brackets[ ]and Tuples are surrounded by parenthesis( ).

List = [1,2,3]
Tuple = (1,2,3)

2. Mutable lists vs immutable tuples

The main difference between lists and tuples is the fact that lists are mutable whereas tuples are immutable.

It means that we can modify a list after it has been initialized i.e. we can add, update or even delete items in a list. But, we cannot change the items in a tuple once it has been created.

# list
List = [1,2,3]
List.append(4)
print(List)# [1, 2, 3, 4]
# tuple
Tuple = (1,2,3)
Tuple.append(4)# AttributeError: 'tuple' object has no attribute 'append'

Python Lists Vs Tuples

In this article we will learn key differences between the List and Tuples and how to use these two data structure.

Lists and Tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type including the nothing type defined by the None Keyword.

Lists and Tuples are similar in most context but there are some differences which we are going to find in this article.


List vs Tuple

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

ListTuple
It is mutableIt 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.

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

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.

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, inlists 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

Python | Difference Between List and Tuple

List and Tuple in Python are the class of data structure. The list is dynamic, whereas the tuple has static characteristics.
List is just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it the most powerful tool in Python. In Python, the list is a type of container in Data Structures, which is used to store multiple data at the same time. Lists are a useful tool for preserving a sequence of data and further iterating over it.
Syntax:

list_data = ['an', 'example', 'of', 'a', 'list']

Tuple is also a sequence data type that can contain elements of different data types, but these are immutable in nature. In other words, a tuple is a collection of Python objects separated by commas. The tuple is faster than the list because of static in nature.
Syntax:

tuple_data = ('this', 'is', 'an', 'example', 'of', 'tuple')

Difference Between List and Tuple in Python:

SR.NO. LIST TUPLE
1 Lists are mutable Tuples are immutable
2 Implication of iterations is Time-consuming The implication of iterations is comparatively Faster
3 The list is better for performing operations, such as insertion and deletion. Tuple data type is appropriate for accessing the elements
4 Lists consume more memory Tuple consume less memory as compared to the list
5 Lists have several built-in methods Tuple does not have many built-in methods.
6 The unexpected changes and errors are more likely to occur In tuple, it is hard to take place.

Difference between List and tuple in Python w3schools

Article Tags :
Difference Between
Python

Python Tuple

Tuple is an ordered sequence of comma-separated values (items or elements). Tuples are same as list, but the only difference is that tuples are immutable. Once a tuple is created their elements and size can not be changed.A Tuple is defined within parentheses () where items are separated by commas.

Example:-

1
2
t1 = ('sun','mon', 'tue')
print t1

Output:-

1
('sun', 'mon', 'tue')

TUPLE IN PYTHON W3SCHOOL

Related Search

› polymorphism in python w3schools

Looking for:

Search Results

Python Tuples - W3Schools

Online www.w3schools.com

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.. A tuple is a collection which is ordered and unchangeable.. Tuples are written with round brackets.

442 People Used

View all course ››

100
❯ Visit Site
Share this result
×

Python Tuples - W3Schools

Copy the link and share

Tap To Copy