Data Structures
Lists and Tuples
Creating and Accessing Lists
Definition: A list is a mutable, ordered collection of items. Lists can contain elements of mixed data types.
Creating a List:
fruits = ["apple", "banana", "cherry"]
Accessing Elements:
By Index:
print(fruits[0]) # Output: "apple"
Using Slicing:
print(fruits[1:3]) # Output: ["banana", "cherry"]
List Methods
Append an Item:
fruits.append("orange")
print(fruits) # Output: ["apple", "banana", "cherry", "orange"]
Insert an Item:
fruits.insert(1, "kiwi")
print(fruits) # Output: ["apple", "kiwi", "banana", "cherry"]
Remove an Item:
fruits.remove("banana")
print(fruits) # Output: ["apple", "kiwi", "cherry"]
Sort the List:
fruits.sort()
print(fruits) # Output: ["apple", "cherry", "kiwi"]
Tuples vs. Lists
Tuples:
- Immutable (cannot be changed after creation).
- Created with parentheses ().
colors = ("red", "green", "blue")
Lists:
- Mutable (can be modified).
- Created with square brackets [].
When to Use:
- Use tuples for fixed collections of data.
- Use lists when data needs to change over time.
person = ("John", 25) # Tuple
shopping_list = ["eggs", "milk", "bread"] # List
Dictionaries and Sets
Creating and Accessing Dictionaries
Definition: A dictionary is an unordered collection of key-value pairs.
student = {"name": "Alice", "age": 22, "grade": "A"}
Accessing Values:
print(student["name"]) # Output: "Alice"
print(student.get("age")) # Output: 22
Dictionary Methods
Add or Update a Key-Value Pair:
student["major"] = "Computer Science"
print(student) # Output: {"name": "Alice", "age": 22, "grade": "A", "major": "Computer Science"}
Remove a Key:
student.pop("grade")
print(student) # Output: {"name": "Alice", "age": 22, "major": "Computer Science"}
Iterate Through a Dictionary:
for key, value in student.items():
print(key, value)
Sets and Their Applications
Definition:
A set is an unordered collection of unique items.
numbers = {1, 2, 3, 4}
Set Operations:
Union:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2) # Output: {1, 2, 3, 4, 5}
Intersection:
print(set1 & set2) # Output: {3}
Difference:
print(set1 - set2) # Output: {1, 2}
Applications:
Removing duplicates from a list:
data = [1, 2, 2, 3, 4, 4, 5]
unique_data = set(data)
print(unique_data) # Output: {1, 2, 3, 4, 5}
Comprehensions in Python
Comprehensions in Python provide a concise way to create lists, dictionaries, and sets. They help make code more readable and efficient by replacing traditional loops with a more compact syntax.
List Comprehensions:
List comprehensions allow you to create a new list by applying an expression to each item in an iterable.
squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
Dictionary Comprehensions:
Dictionary comprehensions allow the creation of dictionaries using a similar syntax to list comprehensions.
square_dict = {x: x**2 for x in range(5)}
print(square_dict) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Set Comprehensions:
Set comprehensions work similarly to list comprehensions but create sets instead of lists.
unique_squares = {x**2 for x in range(5)}
print(unique_squares) # Output: {0, 1, 4, 9, 16}
Summary
- Lists: Mutable, ordered, and versatile.
- Tuples: Immutable and used for fixed collections.
- Dictionaries: Key-value pairs, unordered, and fast for lookups.
- Sets: Unordered collections of unique elements.
- Comprehensions: Provide concise, readable ways to create data structures.