Lists, Tuples, and Sets in Python with Examples
#Lists, Tuples, Sets in Python with Examples
Introduction
When learning Python, understanding Lists, Tuples, and Sets is essential. These are the most commonly used data structures that help developers store, organize, and manage collections of data efficiently.
In this guide, you’ll learn:
What are Lists, Tuples, and Sets in Python?
Their syntax, features, and key differences.
Practical examples for beginners and professionals.
Best practices for choosing the right data structure.
A list is an ordered, mutable (changeable) collection of elements. Lists can store duplicate values and allow indexing and slicing.
# Creating a list
fruits = ["apple", "banana", "cherry"]
# Accessing elements
print(fruits[0]) # Output: apple
# Modifying a list
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
# Removing an item
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry', 'orange']
✅ Key Features of Lists
Ordered
Mutable (can be changed)
Allows duplicates
A tuple is an ordered, immutable (unchangeable) collection of elements. Once created, the values in a tuple cannot be modified.
# Creating a tuple
coordinates = (10, 20, 30)
# Accessing elements
print(coordinates[1]) # Output: 20
# Attempting modification will cause an error
# coordinates[0] = 50 # ❌ TypeError
✅ Key Features of Tuples
Ordered
Immutable
Allows duplicates
Faster than lists due to immutability
A set is an unordered collection of unique elements. It automatically removes duplicates and is useful for membership testing and mathematical operations like union and intersection.
# Creating a set
numbers = {1, 2, 3, 4, 4, 5}
print(numbers) # Output: {1, 2, 3, 4, 5} (duplicates removed)
# Adding an element
numbers.add(6)
print(numbers) # Output: {1, 2, 3, 4, 5, 6}
# Set operations
odd = {1, 3, 5, 7}
even = {2, 4, 6, 8}
print(odd.union(even)) # Output: {1, 2, 3, 4, 5, 6, 7, 8}
print(odd.intersection(numbers)) # Output: {1, 3, 5}
✅ Key Features of Sets
Unordered
Mutable (can add/remove elements)
No duplicates allowed
Useful for mathematical operations
Feature | List | Tuple | Set |
---|---|---|---|
Order | Ordered | Ordered | Unordered |
Mutability | Mutable | Immutable | Mutable |
Duplicates | Allowed | Allowed | Not allowed |
Speed | Slower than tuple | Faster than list | Optimized for lookups |
Use Case | General purpose | Fixed data | Unique values, set ops |
Use lists when you need an ordered, changeable collection.
Use tuples for fixed data that should not change.
Use sets when you want unique elements or need to perform mathematical set operations.
Understanding lists, tuples, and sets in Python is fundamental for any Python developer. By choosing the right data structure for the right use case, you can make your code more efficient, readable, and optimized.
"Learn Lists, Tuples, and Sets in Python with examples. Understand differences, syntax, and best practices for choosing the right data structure in Python programming."
lists in python, tuples in python, sets in python, python data structures, python list example, python tuple example, python set example, list vs tuple vs set in python, python collections tutorial