**Python Data Structures

This lesson introduces you to fundamental Python data structures: lists, dictionaries, and tuples. You'll learn how to create, manipulate, and use these structures to store and organize data effectively, which is crucial for data wrangling and exploration.

Learning Objectives

  • Define and differentiate between lists, dictionaries, and tuples.
  • Create and modify lists using various methods.
  • Create and access elements within dictionaries.
  • Understand the immutability of tuples and their use cases.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Data Structures

Data structures are fundamental building blocks for organizing and storing data in a computer. Python offers several built-in data structures, and in this lesson, we'll focus on three essential ones: lists, dictionaries, and tuples. Each has its own strengths and weaknesses, making them suitable for different tasks. Think of them as different types of containers designed to hold and arrange your data.

Lists: Ordered, Mutable Collections

Lists are ordered collections of items. They are mutable, meaning you can change their contents after they're created. Lists are defined using square brackets [], and elements are separated by commas.

Example:

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

# Accessing elements (indexing starts from 0)
print(my_list[0]) # Output: 1
print(my_list[3]) # Output: 'apple'

# Modifying Lists
my_list[0] = 10  # Changing the first element
print(my_list) # Output: [10, 2, 3, 'apple', 'banana']

# Adding elements
my_list.append("orange")
print(my_list) # Output: [10, 2, 3, 'apple', 'banana', 'orange']

# Removing elements
my_list.remove("apple")
print(my_list) # Output: [10, 2, 3, 'banana', 'orange']

#Slicing
print(my_list[1:3]) # Output: [2,3]

Lists are perfect for storing sequences of data where the order matters and you might need to add, remove, or modify items.

Dictionaries: Key-Value Pairs

Dictionaries are unordered collections of key-value pairs. They are mutable. Each element in a dictionary is stored as a key-value pair, allowing you to quickly look up values using their keys. Dictionaries are defined using curly braces {}.

Example:

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

# Accessing values using keys
print(my_dict["name"]) # Output: Alice
print(my_dict["age"])  # Output: 30

# Adding or modifying elements
my_dict["occupation"] = "Data Scientist"
print(my_dict) # Output: {'name': 'Alice', 'age': 30, 'city': 'New York', 'occupation': 'Data Scientist'}

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

#Checking for key existence
print("age" in my_dict) # Output: True
print("country" in my_dict) # Output: False

Dictionaries are ideal for storing and retrieving data based on meaningful identifiers (keys).

Tuples: Ordered, Immutable Collections

Tuples are ordered collections of items, similar to lists. However, tuples are immutable, meaning you cannot change their contents after creation. They are defined using parentheses (). Because they are immutable, tuples are often used for data that should not be altered.

Example:

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

# Accessing elements (indexing works the same as lists)
print(my_tuple[0]) # Output: 1
print(my_tuple[3]) # Output: 'apple'

# Attempting to modify a tuple will raise an error
# my_tuple[0] = 10  # This will cause a TypeError

# You can concatenate tuples though
new_tuple = my_tuple + (4, 5) # Create a new tuple
print(new_tuple) # Output: (1, 2, 3, 'apple', 'banana', 4, 5)

Tuples are useful when you want to ensure data integrity and when you need to use a collection as a key in a dictionary (since keys must be immutable).

Progress
0%