Data Structures

In this lesson, you'll learn about fundamental data structures in Python: lists, tuples, and dictionaries. Understanding these structures is crucial for organizing and manipulating data efficiently in your data science journey.

Learning Objectives

  • Define and differentiate between lists, tuples, and dictionaries.
  • Create and manipulate lists using various methods (e.g., append, insert, remove).
  • Access and modify data within dictionaries using keys.
  • Understand the key differences between mutable and immutable data structures.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Data Structures

Data structures are fundamental ways of organizing and storing data in a computer so that it can be used efficiently. Python provides several built-in data structures, and in this lesson, we will focus on three key ones: lists, tuples, and dictionaries. Each has its strengths and weaknesses, making them suitable for different tasks. Choosing the right data structure is a critical skill for any data scientist.

Python offers several built-in data structures. We'll be working with three main types in this lesson: lists, tuples, and dictionaries.

Lists: The Flexible Container

Lists are ordered, mutable (changeable) collections of items. They can contain items of different data types. Think of a list like a grocery list where you can add, remove, and rearrange items.

Creating a List:
Lists are defined using square brackets []:

my_list = [1, 2, 3, "apple", "banana"]
print(my_list)  # Output: [1, 2, 3, 'apple', 'banana']

Accessing Elements:
List elements are accessed by their index (starting from 0):

print(my_list[0])  # Output: 1
print(my_list[3])  # Output: apple

Modifying Lists:
Lists are mutable. You can change them using various methods:

  • append(item): Adds an item to the end of the list.
  • insert(index, item): Inserts an item at a specific position.
  • remove(item): Removes the first occurrence of an item.
  • pop(index): Removes and returns the item at a specific index.
  • sort(): Sorts the list (in place).
my_list.append("orange")
print(my_list)  # Output: [1, 2, 3, 'apple', 'banana', 'orange']

my_list.insert(2, "grape")
print(my_list)  # Output: [1, 2, 'grape', 3, 'apple', 'banana', 'orange']

my_list.remove("banana")
print(my_list) # Output: [1, 2, 'grape', 3, 'apple', 'orange']

my_list.sort()
print(my_list) # Output: [1, 2, 3, 'apple', 'grape', 'orange']

removed_item = my_list.pop(0) 
print(removed_item)  # Output: 1
print(my_list) # Output: [2, 3, 'apple', 'grape', 'orange']

Tuples: The Immutable Sequence

Tuples are ordered, immutable (unchangeable) collections of items. Once a tuple is created, you cannot add, remove, or modify its elements. Think of a tuple as a fixed set of values, like coordinates of a point.

Creating a Tuple:
Tuples are defined using parentheses ():

my_tuple = (1, 2, 3, "apple", "banana")
print(my_tuple)  # Output: (1, 2, 3, 'apple', 'banana')

Accessing Elements:
Like lists, elements are accessed by their index:

print(my_tuple[0])  # Output: 1
print(my_tuple[3])  # Output: apple

Immutability:
Trying to modify a tuple will raise an error:

# This will raise an error:
# my_tuple[0] = 4 

When to use Tuples: Use tuples when you want to ensure data integrity (the data shouldn't be changed) or when you want to use them as keys in a dictionary (more on that later).

Dictionaries: Key-Value Pairs

Dictionaries are unordered collections of key-value pairs. Each key must be unique, and it maps to a specific value. Think of a dictionary as a real-world dictionary where you look up a word (key) to find its definition (value).

Creating a Dictionary:
Dictionaries are defined using curly braces {}:

my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print(my_dict)  # Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}

Accessing Values:
Values are accessed using their keys:

print(my_dict["name"])  # Output: Alice
print(my_dict["age"])   # Output: 30

Modifying Dictionaries:
Dictionaries are mutable. You can add, modify, and remove key-value pairs:

my_dict["occupation"] = "Data Scientist"  # Adding a new key-value pair
print(my_dict) # Output: {'name': 'Alice', 'age': 30, 'city': 'New York', 'occupation': 'Data Scientist'}

my_dict["age"] = 31  # Modifying an existing value
print(my_dict) # Output: {'name': 'Alice', 'age': 31, 'city': 'New York', 'occupation': 'Data Scientist'}

del my_dict["city"] # Removing a key-value pair
print(my_dict) # Output: {'name': 'Alice', 'age': 31, 'occupation': 'Data Scientist'}

Mutability vs. Immutability

A crucial concept to understand is mutability.

  • Mutable data structures (like lists and dictionaries) can be changed after they are created. You can add, remove, and modify elements.
  • Immutable data structures (like tuples) cannot be changed after creation. Attempting to change them will result in an error or a new object being created.

Understanding mutability is essential because it affects how you can use and modify your data. Immutable data structures are safer when you don't want the data to be accidentally modified. Mutable data structures are flexible, allowing you to modify them in place.

Progress
0%