Data Structures
Learn about Lists, Tuples, Dictionaries, and Sets.
📋 Topic 1: Lists
A List is a collection which is ordered and changeable (mutable). It allows duplicate members. In Python, lists are written with square brackets [].
🎯 List Characteristics:
- Ordered: Items have a defined order
- Mutable: Can be changed after creation
- Allows duplicates: Same items can appear multiple times
- Indexed: Access items using index positions
💻 List Operations & Methods
💡 Common List Methods:
append()- Add item to endinsert()- Insert item at positionremove()- Remove first occurrencepop()- Remove item at indexsort()- Sort the listreverse()- Reverse the list
🎯 Try It Yourself:
Create a list of numbers and practice operations:
numbers.append(4)
numbers.sort()
numbers.insert(2, 7)
print(numbers)
print("Length:", len(numbers))
🔒 Topic 2: Tuples
A Tuple is a collection which is ordered and unchangeable (immutable). It allows duplicate members. In Python, tuples are written with round brackets ().
⚡ Key Difference:
Lists [] can be changed. Tuples () cannot be changed after creation.
💻 Tuple Operations
💡 When to Use Tuples:
- When you want immutable data
- For fixed collections that shouldn't change
- As dictionary keys (lists can't be keys)
- For data integrity - prevents accidental changes
- Faster than lists for iteration
🎯 Try It Yourself:
Create coordinates as tuples and access them:
point2 = (7, 2)
print("Point 1:", point1)
print("X coordinate:", point1[0])
print("Y coordinate:", point1[1])
# point1[0] = 10 # This would cause an error!
🗂️ Topic 3: Dictionaries
A Dictionary is a collection which is unordered (in older Python versions), changeable (mutable) and indexed. They store data in key: value pairs.
🎯 Dictionary Characteristics:
- Key-Value Pairs: Store data as key:value
- Unordered: No guaranteed order (Python 3.7+ maintains insertion order)
- Mutable: Can be changed after creation
- No Duplicate Keys: Keys must be unique
💻 Dictionary Operations
💡 Common Dictionary Methods:
get()- Safe way to access valueskeys()- Get all keysvalues()- Get all valuesitems()- Get key-value pairsupdate()- Merge dictionariespop()- Remove specific key
🎯 Try It Yourself:
Create a student dictionary and practice operations:
"name": "Priya",
"grade": "A",
"subjects": ["Math", "Science"]
}
student["age"] = 20
print("Name:", student.get("name"))
print("All keys:", student.keys())
🔍 Topic 4: Sets
A Set is a collection which is unordered and unindexed. Most importantly, sets do not allow duplicate members. Sets are written with curly brackets {}.
🎯 Set Characteristics:
- Unordered: No defined order
- No Duplicates: Automatically removes duplicates
- Unindexed: Cannot access by index position
- Mutable: Can add/remove items
- Mathematical Operations: Union, intersection, difference
💻 Set Operations
💡 Common Set Methods:
add()- Add element to setremove()- Remove element (error if not found)discard()- Remove element (no error if not found)union()- Combine setsintersection()- Find common elementsdifference()- Find elements in one set but not other
🎯 Try It Yourself:
Remove duplicates from a list using sets:
unique_numbers = set(numbers)
print("Original:", numbers)
print("Unique:", unique_numbers)
print("Is 3 in set?", 3 in unique_numbers)