Skip to main content
Python for Data Science
CHAPTER 08 Beginner

Tuples, Sets, and Dictionaries

Updated: May 18, 2026
5 min read

# CHAPTER 8

Tuples, Sets, and Dictionaries

1. Chapter Introduction

While Lists are great for ordered sequences, they aren't the right tool for every job. What if you need to store data that should *never* change? What if you need to quickly remove duplicates? What if you need to look up a user's email based on their ID? This chapter introduces the remaining core data structures: Tuples (Immutable), Sets (Unique), and Dictionaries (Key-Value pairs).

2. Tuples: Immutable Sequences

A Tuple is exactly like a list, except it CANNOT be changed (Immutable) after it is created. They use parentheses (). We use tuples for fixed data, like geographic coordinates or RGB colors.

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

# Accessing works just like a list
print(coordinates[0]) # 40.7128

# ERROR! Tuples do not support reassignment
# coordinates[0] = 41.0

3. Sets: Unordered and Unique

A Set is an unordered collection with NO duplicate elements. They use curly braces {}. Sets are incredibly fast for membership testing (in) and mathematically comparing datasets (Unions, Intersections).

python
123456789101112131415
# Creating a set (Notice the duplicates)
user_ids = {101, 102, 103, 101, 101}

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

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

# Set Operations
group_A = {"Alice", "Bob", "Charlie"}
group_B = {"Bob", "Dave"}

print(group_A.intersection(group_B)) # Who is in both? {'Bob'}
print(group_A.union(group_B))        # Combine them all (no dupes)

*Pro-Tip: If you have a messy List with duplicates, convert it to a Set, then back to a List to clean it instantly: clean_list = list(set(messy_list)).*

4. Dictionaries: Key-Value Pairs

A Dictionary maps a "Key" to a "Value", much like a real dictionary maps a word to its definition. They use curly braces {} with a colon : separating the key and value. This is Python's most important structure for dealing with JSON data and Web APIs.

python
1234567891011121314151617
# Creating a dictionary
customer = {
    "id": 554,
    "name": "David",
    "email": "david@example.com"
}

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

# Adding or updating a Key-Value pair
customer["status"] = "Active"
customer["email"] = "dave_new@example.com"

# Getting a list of all keys or values
print(customer.keys())
print(customer.values())

5. Nested Data Structures

In data science, structures are often nested. A JSON response from a web server is usually a List of Dictionaries.

python
1234567891011
database = [
    {"user": "Alice", "role": "Admin", "scores": [90, 95, 88]},
    {"user": "Bob", "role": "Editor", "scores": [70, 75, 80]}
]

# How to access Bob's first score:
# 1. Access the 2nd item in the list (index 1)
# 2. Access the "scores" key in that dictionary
# 3. Access the 1st item in the scores list (index 0)
bobs_first_score = database[1]["scores"][0]
print(f"Bob's first score is {bobs_first_score}") # 70

6. Dictionary Comprehensions

Just like List Comprehensions, you can create dictionaries in a single line.

python
12345
names = ["Alice", "Bob", "Charlie"]

# Create a dictionary mapping the name to the length of the name
name_lengths = {name: len(name) for name in names}
print(name_lengths) # {'Alice': 5, 'Bob': 3, 'Charlie': 7}

7. Mini Project: Student Records Manager

Let's use a dictionary to manage student grades and calculate their average.

python
1234567891011121314151617
# A dictionary where Keys are student names, Values are lists of scores
grade_book = {
    "Alice": [90, 95, 92],
    "Bob": [70, 65, 80],
    "Charlie": [85, 88, 90]
}

# Iterate through the dictionary (.items() gets both key and value)
for student, scores in grade_book.items():
    avg = sum(scores) / len(scores)
    
    if avg >= 90:
        status = "Honors"
    else:
        status = "Pass"
        
    print(f"{student}: Avg = {avg:.1f} | Status: {status}")

8. Common Mistakes

  • Indexing a Set: Because Sets are unordered, they don't have indexes. my_set[0] will throw an error.
  • KeyError in Dictionaries: If you try to access customer["phone"] but the "phone" key doesn't exist, Python crashes. Use the .get() method instead: customer.get("phone", "Not Found") returns "Not Found" instead of crashing.

9. MCQs

Question 1

Which data structure is Immutable (cannot be changed)?

Question 2

Which symbols are used to define a Tuple?

Question 3

If you need to quickly remove duplicates from a massive list, you should convert it to a:

Question 4

What is the output of {1, 1, 2, 2, 3}?

Question 5

Dictionaries store data in what format?

Question 6

How do you access the value associated with the key "age" in a dictionary named user?

Question 7

What method should you use to access a dictionary key safely without causing a KeyError if it doesn't exist?

Question 8

Which of the following is an example of a Set?

Q9. In a dictionary, can you have duplicate Keys? a) Yes b) No, keys must be unique. If you reuse a key, it overwrites the old value. — Answer: b
Question 10

How do you loop through both the keys and values of a dictionary simultaneously?

10. Interview Questions

  • Q: Explain the difference between a List and a Set. Give a real-world scenario where you would use a Set.
  • Q: You have a dictionary of 10,000 customers. What happens if you try to access a key that doesn't exist? How do you write defensive code to prevent a crash?

11. Summary

Python provides data structures for every scenario. Use Lists [] for ordered, changing data. Use Tuples () to protect fixed data from being altered. Use Sets {} to guarantee uniqueness and perform fast mathematical comparisons. Finally, use Dictionaries {k:v} to map relationships and handle JSON data from the web.

12. Next Chapter Recommendation

In Chapter 9: File Handling in Python, we will learn how to read data from your hard drive into these structures, and write your analysis back out to text, CSV, and JSON files.

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: ·