Differences Between List and Tuple in Python

Last Updated : 01/30/2025 15:08:09

This article will explain the difference between a list and a tuple in Python. By the end of this article, you will be adept in syntax differences, available operations, and scenarios of using lists and tuples in Python.

Differences Between List and Tuple in Python
The main difference between tuples and lists is that tuples are immutable, meaning their contents cannot be changed after creation, while lists are mutable and can be modified. Additionally, tuples are more memory-efficient compared to lists.


What is a List in Python?

A list in Python is an ordered, mutable (changeable), and iterable collection used to store multiple items in a single variable. Lists can contain elements of different data types (integers, strings, floats, even other lists).

Key Features of a List :

* Ordered – Items are stored in a specific order and can be accessed using an index.
* Mutable – You can modify a list by adding, removing, or changing elements.
* Allows Duplicates – Lists can contain duplicate values.
* Heterogeneous – A list can store different data types (integers, strings, lists, etc.).
* Dynamic Size – Lists can grow or shrink dynamically as elements are added or removed.

Creating a List :

Empty List :
my_list = []
List with Elements :
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", 3.14, True]

Accessing List Elements :

Using Indexing (Starts at 0 :
fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Output: apple
print(fruits[-1]) # Output: cherry (negative index counts from the end)

 

Using Slicing :

numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])  # Output: [20, 30, 40]
print(numbers[:3])   # Output: [10, 20, 30]
print(numbers[-3:])  # Output: [30, 40, 50]

 

Modifying a List (Mutable) :

1. Adding Elements :
fruits.append("orange")  # Adds at the end
fruits.insert(1, "grape") # Inserts at index 1
print(fruits)  # Output: ['apple', 'grape', 'banana', 'cherry', 'orange']?

2. Removing Elements :
fruits.remove("banana")  # Removes "banana"
popped_item = fruits.pop(2)  # Removes element at index 2
del fruits[0]  # Deletes first element
fruits.clear()  # Empties the list?

3. Updating Elements :
fruits[1] = "blueberry"  # Change "banana" to "blueberry"?


Looping Through a List :


Using a For Loop :
for fruit in fruits:
    print(fruit)?


Using List Comprehension :
squared_numbers = [x**2 for x in [1, 2, 3, 4, 5]]
print(squared_numbers)  # Output: [1, 4, 9, 16, 25]?


Common List Methods :


Method Description Example
append(x) Adds an element to the end list.append(10)
insert(i, x) Inserts an element at index i list.insert(1, 20)
remove(x) Removes first occurrence of x list.remove(5)
pop(i) Removes element at index i list.pop(2)
clear() Removes all elements list.clear()
sort() Sorts the list (ascending by default) list.sort()
reverse() Reverses the list order list.reverse()
count(x) Counts occurrences of x list.count(2)
index(x) Returns index of first occurrence of x list.index(3)


Example Use Case :

students = ["Alice", "Bob", "Charlie"]
students.append("David")
students.remove("Bob")
print(students)  # Output: ['Alice', 'Charlie', 'David']?


What is a Tuple in Python?

A tuple in Python is an ordered, immutable (unchangeable), and iterable collection used to store multiple items in a single variable. Unlike lists, tuples cannot be modified after creation, making them useful for storing fixed data.

Key Features of a Tuple :

* Ordered – Items maintain a specific sequence and can be accessed via indexing.
* Immutable – Once created, elements cannot be changed, added, or removed.
* Allows Duplicates – Can store repeated values.
* Heterogeneous – Can store multiple data types (integers, strings, lists, etc.).
* Memory Efficient & Faster – Uses less memory and is faster than lists due to immutability.
* Hashable – Tuples can be used as dictionary keys if they contain only immutable elements.

Creating a Tuple :

1. Empty Tuple :
empty_tuple = ()
empty_tuple = tuple()  # Another way?

2. Tuple with Elements :
fruits = ("apple", "banana", "cherry")
numbers = (1, 2, 3, 4, 5)
mixed_tuple = (1, "hello", 3.14, True)?

3. Single-Element Tuple (Comma is Required!) :
single_element_tuple = ("hello",)  # ? Correct
not_a_tuple = ("hello")  # ? This is just a string?

 

Accessing Tuple Elements :


1. Using Indexing (Starts at 0) :
fruits = ("apple", "banana", "cherry")
print(fruits[0])  # Output: apple
print(fruits[-1]) # Output: cherry (negative index counts from the end)?

2. Using Slicing :
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])  # Output: (20, 30, 40)
print(numbers[:3])   # Output: (10, 20, 30)
print(numbers[-3:])  # Output: (30, 40, 50)?

Tuple Immutability (Cannot be Modified) :

fruits = ("apple", "banana", "cherry")
fruits[1] = "blueberry"  # ? TypeError: 'tuple' object does not support item assignment?

 

Tuple Operations :

1. Concatenation (Merging Tuples) :
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
merged_tuple = tuple1 + tuple2
print(merged_tuple)  # Output: (1, 2, 3, 4, 5, 6)?


2. Repetition :
numbers = (1, 2, 3)
repeated = numbers * 3
print(repeated)  # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)?
 
3. Tuple Unpacking :
person = ("John", 30, "Engineer")
name, age, profession = person
print(name)       # Output: John
print(age)        # Output: 30
print(profession) # Output: Engineer?



Tuple Methods :


Method Description Example
count(x) Counts occurrences of x tuple.count(2)
index(x) Returns index of first occurrence of x tuple.index(3)


When to Use Tuples Instead of Lists?

* When data should not be modified (e.g., days of the week, fixed coordinates).
* When you need faster access and iteration (tuples are faster than lists).
* When using dictionary keys (tuples are hashable if they contain only immutable elements).


Differences Between List and Tuple in Python :


Feature List Tuple
Mutability Mutable (can be modified) ? Immutable (cannot be modified after creation) ?
Syntax Defined using square brackets [ ] Defined using parentheses ( )
Performance Slower due to dynamic resizing and modification overhead ? Faster due to immutability and fixed structure ?
Memory Usage Takes more memory ? Takes less memory due to immutability ?
Operations Supports append(), remove(), pop(), sort(), etc. Limited operations (count(), index())
Use Case Suitable for dynamic data where modification is needed (e.g., lists of users, tasks, etc.) Suitable for fixed data that should not change (e.g., coordinates, database records, constants)
Iteration Speed Slower because of modifiable structure Faster due to fixed size
Hashability Not hashable (cannot be used as dictionary keys) ? Hashable if all elements inside are immutable ?


Example Usage :

List Example (Mutable) :
my_list = [1, 2, 3]
my_list.append(4)  # ? Allowed
my_list[0] = 100   # ? Allowed
print(my_list)  # Output: [100, 2, 3, 4]?

Tuple Example (Immutable) :
my_tuple = (1, 2, 3)
my_tuple[0] = 100  # ? Error: TypeError: 'tuple' object does not support item assignment?

Note : This article is only for students, for the purpose of enhancing their knowledge. This article is collected from several websites, the copyrights of this article also belong to those websites like : Newscientist, Techgig, simplilearn, scitechdaily, TechCrunch, TheVerge etc,.