Difference Between Set vs List vs Tuple

Python provides three essential data structures for storing collections: lists, tuples, and sets. Each serves different purposes with unique characteristics that make them suitable for specific use cases.

List

A list is a mutable, ordered collection that allows duplicate elements. Items can be changed, added, or removed after creation ?

# Create a list
fruits = ['apple', 'banana', 'orange']
print("Original list:", fruits)

# Access elements by index
print("First fruit:", fruits[0])

# Add elements
fruits.append('kiwi')
print("After append:", fruits)

# Remove elements
fruits.remove('banana')
print("After removal:", fruits)

# Check membership
print("Is 'apple' in list?", 'apple' in fruits)

# List length
print("Length:", len(fruits))
Original list: ['apple', 'banana', 'orange']
First fruit: apple
After append: ['apple', 'banana', 'orange', 'kiwi']
After removal: ['apple', 'orange', 'kiwi']
Is 'apple' in list? True
Length: 3

Tuple

A tuple is an immutable, ordered collection that allows duplicate elements. Once created, you cannot modify its contents ?

# Create a tuple
coordinates = (10, 20, 30)
print("Tuple:", coordinates)

# Access elements by index
print("X coordinate:", coordinates[0])

# Check membership
print("Is 20 in tuple?", 20 in coordinates)

# Tuple length
print("Length:", len(coordinates))

# Tuples are immutable - this would cause an error:
# coordinates[0] = 15  # TypeError!
Tuple: (10, 20, 30)
X coordinate: 10
Is 20 in tuple? True
Length: 3

Set

A set is a mutable, unordered collection that stores only unique elements. No duplicates are allowed ?

# Create a set
numbers = {1, 2, 3, 4, 4, 5}  # Note: duplicate 4 is ignored
print("Set:", numbers)

# Add elements
numbers.add(6)
print("After adding 6:", numbers)

# Remove elements
numbers.discard(3)
print("After removing 3:", numbers)

# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print("Union:", set1.union(set2))
print("Intersection:", set1.intersection(set2))
print("Difference:", set1.difference(set2))
Set: {1, 2, 3, 4, 5}
After adding 6: {1, 2, 3, 4, 5, 6}
After removing 3: {1, 2, 4, 5, 6}
Union: {1, 2, 3, 4, 5}
Intersection: {3}
Difference: {1, 2}

Comparison

Feature List Tuple Set
Mutable Yes No Yes
Ordered Yes Yes No
Duplicates Allowed Allowed Not allowed
Indexing Yes Yes No
Syntax [ ] ( ) { }

When to Use Each

Use Lists when:

  • You need a mutable, ordered collection

  • Order matters and you need indexing

  • Duplicates are acceptable

Use Tuples when:

  • You need an immutable collection (coordinates, database records)

  • Data should not change after creation

  • Using as dictionary keys (since they're hashable)

Use Sets when:

  • You need unique elements only

  • Performing mathematical set operations

  • Fast membership testing is required

Conclusion

Choose lists for generalpurpose collections, tuples for immutable data, and sets for unique elements and fast lookups. Understanding these differences helps you write more efficient Python code.

Updated on: 2026-03-27T01:17:53+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements