Python Data Structures Explained: Lists, Tuples, Sets, and Dictionaries

Table Of Content
- Python Data Structures Explained: Lists, Tuples, Sets, and Dictionaries
- 1. **Lists**
- What is a List?
- Creating a List
- Common List Operations
- 2. **Tuples**
- What is a Tuple?
- Creating a Tuple
- Common Tuple Operations
- 3. **Sets**
- What is a Set?
- Creating a Set
- Common Set Operations
- 4. **Dictionaries**
- What is a Dictionary?
- Creating a Dictionary
- Common Dictionary Operations
- When to Use Which Data Structure?
- Next Steps
Python Data Structures Explained: Lists, Tuples, Sets, and Dictionaries
Python provides several built-in data structures to store and manipulate collections of data. In this guide, we'll explore the four most commonly used data structures: lists, tuples, sets, and dictionaries.
1. Lists
What is a List?
A list is an ordered, mutable collection of items. Lists can store elements of different data types.
Creating a List
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "apple", 3.14, True]
Common List Operations
- Accessing Elements:
print(fruits[0]) # Output: apple
- Slicing:
print(numbers[1:3]) # Output: [2, 3]
- Adding Elements:
fruits.append("orange")
- Removing Elements:
fruits.remove("banana")
- Length of List:
print(len(fruits)) # Output: 3
2. Tuples
What is a Tuple?
A tuple is an ordered, immutable collection of items. Once created, its elements cannot be changed.
Creating a Tuple
coordinates = (10, 20)
colors = ("red", "green", "blue")
Common Tuple Operations
- Accessing Elements:
print(coordinates[0]) # Output: 10
- Slicing:
print(colors[1:3]) # Output: ("green", "blue")
- Length of Tuple:
print(len(colors)) # Output: 3
3. Sets
What is a Set?
A set is an unordered collection of unique elements. Sets are useful for removing duplicates and performing mathematical operations like union and intersection.
Creating a Set
unique_numbers = {1, 2, 3, 4, 5}
vowels = {"a", "e", "i", "o", "u"}
Common Set Operations
- Adding Elements:
unique_numbers.add(6)
- Removing Elements:
unique_numbers.remove(3)
- Set Operations:
set1 = {1, 2, 3} set2 = {3, 4, 5} print(set1.union(set2)) # Output: {1, 2, 3, 4, 5} print(set1.intersection(set2)) # Output: {3}
4. Dictionaries
What is a Dictionary?
A dictionary is an unordered collection of key-value pairs. Keys must be unique and immutable (e.g., strings, numbers).
Creating a Dictionary
person = {"name": "Alice", "age": 25, "city": "New York"}
Common Dictionary Operations
- Accessing Values:
print(person["name"]) # Output: Alice
- Adding/Updating Elements:
person["email"] = "alice@example.com"
- Removing Elements:
del person["age"]
- Length of Dictionary:
print(len(person)) # Output: 3
When to Use Which Data Structure?
- Lists: Use when you need an ordered, mutable collection of items.
- Tuples: Use when you need an ordered, immutable collection of items.
- Sets: Use when you need to store unique elements or perform set operations.
- Dictionaries: Use when you need to store data as key-value pairs for quick lookups.
Next Steps
Now that you understand Python's core data structures, here are some next steps:
- Practice using lists, tuples, sets, and dictionaries in small projects.
- Explore advanced data structures like stacks, queues, and linked lists.
- Learn how to combine these data structures for complex data manipulation.
Python's data structures are powerful tools for organizing and processing data. Happy coding!