Welcome to Day 5 of the 30 Days of Data Science series! Today, we will explore data structures in Python, focusing on three important types:
- Lists
- Tuples
- Dictionaries
Understanding these data structures is fundamental for data manipulation and organization in Python.
- π Day 5: Data Structures in Python
A list is a mutable (modifiable) collection of ordered elements. Lists can store elements of different data types, such as integers, strings, floats, or even other lists.
# Creating a list of numbers
numbers = [1, 2, 3, 4, 5]
# Creating a mixed data type list
mixed_list = [1, "apple", 3.14, True]
You can access elements in a list using indexing (zero-based).
fruits = ["apple", "banana", "cherry"]
# Accessing the first element
print(fruits[0]) # Output: apple
# Accessing the last element
print(fruits[-1]) # Output: cherry
Use the append()
method to add an element to the end or the insert()
method to add at a specific position.
fruits = ["apple", "banana"]
# Adding an element at the end
fruits.append("cherry")
print(fruits) # Output: ['apple', 'banana', 'cherry']
# Inserting an element at a specific position
fruits.insert(1, "orange")
print(fruits) # Output: ['apple', 'orange', 'banana', 'cherry']
Use remove()
or pop()
to delete elements.
fruits = ["apple", "banana", "cherry"]
# Removing by value
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry']
# Removing by index
fruits.pop(1)
print(fruits) # Output: ['apple']
Slicing allows you to access a subset of elements.
numbers = [1, 2, 3, 4, 5]
# Getting the first three elements
print(numbers[:3]) # Output: [1, 2, 3]
# Getting elements from index 2 to the end
print(numbers[2:]) # Output: [3, 4, 5]
Here are some commonly used list methods:
numbers = [1, 2, 3]
# Adding an element
numbers.append(4)
# Counting occurrences
print(numbers.count(2)) # Output: 1
# Sorting the list
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4]
A tuple is an immutable (unchangeable) collection of ordered elements. Tuples are often used to group related data.
# Creating a tuple of strings
fruits = ("apple", "banana", "cherry")
Similar to lists, you can access tuple elements using indexing.
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Output: apple
Tuples cannot be changed after creation. Attempting to modify a tuple results in an error.
fruits = ("apple", "banana", "cherry")
# This will raise an error
fruits[1] = "orange"
fruits = ("apple", "banana", "cherry")
# Getting the index of an element
print(fruits.index("banana")) # Output: 1
# Counting occurrences
print(fruits.count("cherry")) # Output: 1
A dictionary is a mutable collection of key-value pairs. Keys must be unique and immutable, while values can be of any type.
# Creating a dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
You can access values using keys.
person = {"name": "Alice", "age": 25}
print(person["name"]) # Output: Alice
person = {"name": "Alice", "age": 25}
# Adding a new key-value pair
person["city"] = "New York"
# Updating an existing key
person["age"] = 26
Use the del
keyword or pop()
method.
person = {"name": "Alice", "age": 25}
# Removing a key-value pair
del person["age"]
# Using pop()
person.pop("name")
person = {"name": "Alice", "age": 25}
# Getting all keys
print(person.keys()) # Output: dict_keys(['name', 'age'])
# Getting all values
print(person.values()) # Output: dict_values(['Alice', 25])
- Create a list of your favorite movies and print the last one using negative indexing.
- Create a tuple of three numbers and calculate their sum.
- Create a dictionary to store information about a book (title, author, year), and add the publisher's name.
- Lists are mutable and ordered collections.
- Tuples are immutable and ordered collections.
- Dictionaries store data as key-value pairs and are mutable.