Skip to main content
Jupyter Notebooks
CHAPTER 08 Beginner

Data Structures in Jupyter

Updated: May 18, 2026
5 min read

# CHAPTER 8

Data Structures in Jupyter

1. Chapter Introduction

Variables hold a single piece of information, like age = 25. But what if you need to store the ages of 10,000 customers? You need a Data Structure. Data structures are containers that organize and store data efficiently. This chapter covers Python's four built-in data structures—Lists, Tuples, Sets, and Dictionaries—and how to interact with them in Jupyter.

2. Lists: Ordered and Mutable

A List is a collection of items in a specific order. You can change, add, or remove items after the list is created (Mutable). They are defined using square brackets [].

Cell 1:

python
123456789101112
# Creating a list
fruits = ["Apple", "Banana", "Cherry"]
print(fruits)

# Accessing items via Index (Python is 0-indexed!)
print(fruits[0])  # Output: Apple
print(fruits[-1]) # Output: Cherry (Negative index gets the last item)

# Modifying the list
fruits.append("Orange") # Adds to the end
fruits[1] = "Blueberry" # Changes "Banana" to "Blueberry"
print(fruits)

3. Tuples: Ordered and Immutable

A Tuple is exactly like a list, except it CANNOT be changed after it is created (Immutable). They are defined using parentheses (). Use tuples for data that should never be altered (like geographic coordinates).

Cell 2:

python
123456789
# Creating a tuple
coordinates = (40.7128, -74.0060)

# Accessing works the same as lists
print(coordinates[0]) # Output: 40.7128

# This will cause an ERROR:
# coordinates[0] = 41.0000 
# TypeError: 'tuple' object does not support item assignment

4. Sets: Unordered and Unique

A Set is an unordered collection with NO duplicate elements. They are defined using curly braces {}. Sets are incredibly fast for checking if an item exists or for removing duplicates from a list.

Cell 3:

python
12345678910111213
# Creating a set
user_ids = {101, 102, 103, 101, 101}

# Notice the duplicates are automatically removed!
print(user_ids) # Output: {101, 102, 103}

# Fast membership testing
print(102 in user_ids) # Output: True

# Removing duplicates from a list using a set
messy_list = ["A", "B", "A", "C"]
clean_list = list(set(messy_list))
print(clean_list) # Output: ['A', 'C', 'B'] (Order is not guaranteed)

5. Dictionaries: Key-Value Pairs

A Dictionary stores data in Key-Value pairs, just like a real dictionary maps a word to its definition. They are defined using curly braces {}, with a colon : separating the key and value. Dictionaries are the most important data structure for interacting with web APIs (JSON).

Cell 4:

python
123456789101112131415
# Creating a dictionary
customer = {
    "id": 554,
    "name": "David",
    "is_active": True
}

# Accessing data using the Key
print(customer["name"]) # Output: David

# Adding or updating a Key-Value pair
customer["email"] = "david@example.com"
customer["is_active"] = False

print(customer)

6. Nested Data Structures

In real-world data science, structures are often nested inside each other. For example, a List of Dictionaries.

Cell 5:

python
1234567891011
# A list of dictionaries (Very common for database results)
database = [
    {"user": "Alice", "role": "Admin"},
    {"user": "Bob", "role": "Editor"}
]

# Accessing Bob's role:
# 1. Access index 1 of the list (Bob's dictionary)
# 2. Access the "role" key of that dictionary
bobs_role = database[1]["role"]
print(bobs_role) # Output: Editor

7. Jupyter Display Magic

In Jupyter, if you put a large dictionary or list on the last line of a cell, Jupyter will format it beautifully (often with syntax highlighting) instead of printing a massive, unreadable block of text.

Cell 6:

python
12345
# Don't use print() for large structures at the end of a cell
large_dict = {"A": [1,2,3], "B": [4,5,6], "C": {"nested": "value"}}

# Just type the variable name
large_dict

8. Common Mistakes

  • Zero-Indexing Confusion: Beginners often try to get the first item in a list using fruits[1]. In Python, the first item is ALWAYS index 0. fruits[1] gets the *second* item.
  • Trying to order Sets: Because Sets are unordered, you cannot use an index on them. my_set[0] will cause an error.

9. MCQs

Question 1

Which data structure uses square brackets [] and allows you to change its contents?

Question 2

How do you access the *first* item in a Python list named data?

Question 3

Which data structure uses parentheses () and CANNOT be modified after creation?

Question 4

If you have a list with duplicates and you want to quickly remove them, you can convert the list into a:

Question 5

Dictionaries store data in what format?

Question 6

How do you access the value "Alice" in this dictionary: user = {"name": "Alice"}?

Question 7

What happens if you try to append a new item to a Tuple?

Question 8

Which index number accesses the *last* item in a list?

Q9. Is a Set ordered? Can you rely on the items staying in the exact same position? a) Yes b) No — Answer: b
Question 10

Why is it better to just type a large dictionary's variable name on the last line of a Jupyter cell instead of using print()?

10. Interview Questions

  • Q: Explain the difference between a List and a Tuple. When would you choose to use a Tuple over a List?
  • Q: You have a list of 100,000 employee names, and you need to check if "Alice" is in the list. To optimize for speed, which data structure should you convert the list into before checking?

11. Summary

Python provides four core data structures. Lists [] are ordered and mutable, perfect for sequences of data. Tuples () are ordered and immutable, used for fixed data. Sets {} are unordered and unique, used for fast membership testing and deduplication. Dictionaries {"key": "value"} are mapped structures essential for complex data storage.

12. Next Chapter Recommendation

In Chapter 9: File Handling and Notebook Management, we will learn how to read data from your hard drive into these structures, write results to new files, and keep your Jupyter project folders organized.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·