Skip to content

Difference between list and tuple in Python | Basics

  • by

The difference between a list and a tuple is list is dynamic, whereas a tuple has static characteristics in Python.

Lists are a useful tool for preserving a sequence of data and further iterating over it and the tuple is faster than the list because of static in nature.

List Syntax

list_data = ['a', 'b', 'c', 'd', 'e']

Tuple Syntax

tuple_data = ('a', 'b', 'c', 'd', 'e', 'f')

In Python, both lists and tuples are used to store collections of items, but they have some key differences:

  1. Mutability: Lists are mutable, which means you can modify, add, or remove elements after creating them. Tuples, on the other hand, are immutable, meaning that once created, you cannot modify their elements.
  2. Syntax: Lists are defined using square brackets [], while tuples use parentheses ().
  3. Usage: Lists are typically used for collections where the order and the elements may change over time, and where you need to perform operations like appending, extending, or removing items. Tuples, on the other hand, are used for fixed collections where the order and the values of the elements are important and should not change.
  4. Performance: Tuples are generally more efficient in terms of performance and memory usage compared to lists because of their immutability. Since tuples cannot be modified, Python can optimize them for better memory storage and faster access.
  5. Common Operations: Both lists and tuples can be indexed and sliced using the same syntax. For example, my_list[0] would access the first element in a list, while my_tuple[0] would do the same for a tuple. However, since lists are mutable, you can assign new values to specific indices, while this is not possible with tuples.

Tabular format difference between list and tuple in Python

The table below includes the basic difference

#LISTTUPLE
1Lists are mutableTuples are immutable
2The implication of iterations is Time-consumingThe implication of iterations is comparatively Faster
3The list is better for performing operations, such as insertion and deletion.Tuple consumes less memory as compared to the list
4Lists consume more memoryUnexpected changes and errors are more likely to occur
5Lists have several built-in methodsTuple does not have many built-in methods.
6In a tuple, it is hard to take place.In tuple, it is hard to take place.

Example

my_list = [1, 2, 3]  # A mutable list
my_list.append(4)    # Modifying the list: [1, 2, 3, 4]
my_list[0] = 0       # Modifying an element: [0, 2, 3, 4]

my_tuple = (1, 2, 3)  # An immutable tuple
# Trying to modify a tuple will result in an error:
# my_tuple[0] = 0

# Both lists and tuples support indexing and slicing
print(my_list[0])    # Output: 0
print(my_tuple[1:])  # Output: (2, 3)

Output:

difference between list and tuple in Python

Comment if you have any doubts or suggestions on this Python difference topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading