List vs tuple vs dictionary in Python

Python provides three fundamental data structures for storing collections of data: lists, tuples, and dictionaries. Each serves different purposes and has unique characteristics that make them suitable for specific use cases.

What is a List?

List is a mutable data structure that stores multiple values in an ordered sequence. Lists are created using square brackets [] and can hold elements of different data types separated by commas.

Lists support indexing (both positive and negative) and can be modified after creation by adding, removing, or changing elements.

Syntax

variable_name = [e1, e2, e3]

Where variable_name is the list name and e1, e2, e3 are the elements.

Example

Creating a list with mixed data types ?

numbers = [1, 2, 3, 4, 5, 6]
print("List:", numbers)
print("Type:", type(numbers))

# Lists are mutable - we can modify them
numbers.append(7)
print("After append:", numbers)
List: [1, 2, 3, 4, 5, 6]
Type: <class 'list'>
After append: [1, 2, 3, 4, 5, 6, 7]

What is a Tuple?

Tuple is an immutable data structure that stores multiple values in an ordered sequence. Tuples are created using parentheses () and can contain elements of different data types.

Once created, tuples cannot be modified. They support indexing and allow duplicate elements.

Syntax

variable_name = (e1, e2, e3)

Example

Creating a tuple and demonstrating immutability ?

coordinates = ('a', 'b', 10, 30, 14, 7)
print("Tuple:", coordinates)
print("Type:", type(coordinates))

# Tuples are immutable - this will cause an error
try:
    coordinates[0] = 'x'
except TypeError as e:
    print("Error:", e)
Tuple: ('a', 'b', 10, 30, 14, 7)
Type: <class 'tuple'>
Error: 'tuple' object does not support item assignment

What is a Dictionary?

Dictionary is a mutable data structure that stores data as key-value pairs. Dictionaries are created using curly braces {} and are also known as associative arrays or hash maps.

Each key must be unique and immutable (strings, numbers, tuples), while values can be of any data type and duplicated. Data is accessed using keys rather than indices.

Syntax

variable_name = {k1: v1, k2: v2}

Example

Creating and accessing dictionary elements ?

student = {'name': 'John', 'age': 20, 'grade': 'A'}
print("Dictionary:", student)
print("Type:", type(student))

# Access value by key
print("Name:", student['name'])

# Add new key-value pair
student['city'] = 'New York'
print("Updated:", student)
Dictionary: {'name': 'John', 'age': 20, 'grade': 'A'}
Type: <class 'dict'>
Name: John
Updated: {'name': 'John', 'age': 20, 'grade': 'A', 'city': 'New York'}

Comparison Table

Feature List Tuple Dictionary
Symbol [ ] ( ) { }
Mutability Mutable Immutable Mutable
Ordering Ordered Ordered Ordered (Python 3.7+)
Data Structure Sequential Sequential Key-Value pairs
Access Method Index Index Key
Duplicates Allowed Allowed Keys: No, Values: Yes

Combined Example

Here's a practical example showing all three data structures ?

# List - mutable, ordered collection
fruits = ['apple', 'banana', 'orange']
print("List:", fruits)

# Tuple - immutable, ordered collection  
coordinates = (10.5, 20.3, 15.7)
print("Tuple:", coordinates)

# Dictionary - key-value pairs
person = {'name': 'Alice', 'age': 25, 'city': 'Boston'}
print("Dictionary:", person)

# Demonstrate mutability
fruits.append('grape')  # Works - list is mutable
person['job'] = 'Engineer'  # Works - dict is mutable
# coordinates.append(30)  # Would fail - tuple is immutable

print("Modified list:", fruits)
print("Modified dictionary:", person)
List: ['apple', 'banana', 'orange']
Tuple: (10.5, 20.3, 15.7)
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'Boston'}
Modified list: ['apple', 'banana', 'orange', 'grape']
Modified dictionary: {'name': 'Alice', 'age': 25, 'city': 'Boston', 'job': 'Engineer'}

When to Use Each

Use Lists when: You need a mutable collection that maintains order and allows duplicates.

Use Tuples when: You need an immutable collection for data that shouldn't change (coordinates, RGB values, database records).

Use Dictionaries when: You need to associate keys with values for fast lookups and data organization.

Conclusion

Lists offer flexibility with mutability, tuples provide immutable ordered data, and dictionaries enable efficient key-based data access. Choose based on whether you need mutability, ordering, and how you plan to access the data.

Updated on: 2026-03-24T20:07:16+05:30

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements