Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

tuple
list
python

Tuples vs. List in Python

Educative Answers Team

Tuple and List are two data structures in Python that can store data. Both data structures store data in a specific order of any type (e.g. integers or strings).

svg viewer

Comparison between a Tuple and a List:

Tuple List
A tuple consists of immutable objects. (Objects which cannot change after creation) A list consists of mutable objects. (Objects which can be changed after creation)
Tuple has a small memory. List has a large memory.
Tuple is stored in a single block of memory. List is stored in two blocks of memory (One is fixed sized and the other is variable sized for storing data)
Creating a tuple is faster than creating a list. Creating a list is slower because two memory blocks need to be accessed.
An element in a tuple cannot be removed or replaced. An element in a list can be removed or replaced.
A tuple has data stored in () brackets. For example, (1,2,3) A list has data stored in [] brackets. For example, [1,2,3]

When each is used:

A tuple should be used whenever the user is aware of what is inserted in the tuple. Suppose that a college stores the information of its students in a data structure; in order for this information to remain immutable it should be stored in a tuple.

Since lists provide users with easier accessibility, they should be used whenever similar types of objects need to be stored. For instance, if a grocery needs to store all the dairy products in one field, it should use a list.

Example:

This program stores data in a tuple and in a list; it then displays the size of both data structures.

tuple_A = ('Name = John Cleverly','Grade = Grade 2','ID = 123', 'Section = B')
list_B  = ['Milk','Butter', 'Dessert', 'Ice cream']

print'Tuple data is : ', tuple_A
print'List data is : ', list_B

print'size of a tuple is : ',tuple_A.__sizeof__()
print'size of a list is : ',list_B.__sizeof__()

Despite the fact that there are more characters stored in the tuple, the list is bigger.

RELATED TAGS

tuple
list
python
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring