Chapter 8: Tuples and Dictionaries - More Ways to Organize Data

In the last chapter, you learned all about lists, which are incredibly useful for storing ordered collections of items. However, Python offers other data structures that are better suited for specific tasks. In this chapter, we'll explore two of them: tuples and dictionaries.

Think of these as specialized tools. You can often get a job done with a basic tool (like a list), but sometimes a specialized tool makes the job safer, faster, and easier.

Tuples: The "Can't Change" List

A tuple (pronounced tuh-pull or too-pull) is almost identical to a list. It's an ordered collection of items. The one, huge difference is that a tuple is immutable. This means once you create a tuple, you cannot change, add, or remove items.

You create a tuple using parentheses () instead of square brackets.

# A tuple of numbers
point_in_space = (10, 20, 30) # Represents an (x, y, z) coordinate

# A tuple with mixed types
person_data = ("Alice", 30, "Software Engineer")

print(point_in_space)
print(person_data)

So, Why Use a Tuple?

If tuples are just limited lists, why bother using them?

  1. Safety: Sometimes you have data that should never change. Using a tuple ensures that you or another programmer don't accidentally modify it. For example, the coordinates for a fixed point, or the RGB values for the color red (255, 0, 0).

  2. Performance: Because they are simpler, tuples can be slightly faster for Python to process than lists.

Accessing Tuple Items

You access items in a tuple the same way you do with a list, using indexes.

What You Can't Do

If you try to change an item in a tuple, Python will give you an error.

This is the "immutability" in action. Python is protecting the data from being changed.

Dictionaries: The "Look-up" Collection

Now for something completely different. Lists and tuples are ordered sequences where you access items by their numerical position (index 0, 1, 2, etc.). But what if you want to store data and look it up using a custom label instead of a number?

Think of a real-world dictionary. You don't find a word by its page number; you look it up by the word itself. A Python dictionary works the same way. It's a collection of key-value pairs.

You create dictionaries using curly braces {}.

  • "name", "phone", and "email" are the keys. They must be unique.

  • "Alice", "555-1234", and "[email protected]" are the values.

Accessing, Adding, and Modifying

You use the keys to work with the values.

Removing Items

You can remove items using the del keyword with the key.

Looping Through a Dictionary

Looping with dictionaries is very powerful. The best way is to use the .items() method, which gives you both the key and the value in each iteration.

Output:

Summary and What's Next

You've added two more powerful data structures to your Python toolkit. Let's recap when to use each one:

  • List []: The general-purpose workhorse. An ordered, changeable collection of items. Use this by default.

  • Tuple (): An ordered, un-changeable collection. Use this when you want to make sure data remains constant.

  • Dictionary {}: A collection of key-value pairs. Use this when you need to look up information based on a specific label or identifier.

You are becoming very skilled at handling and organizing data. The next step in our journey is to learn how to organize our code. We will learn how to write functions, which are reusable blocks of code that can perform specific tasks. This will make our programs much more organized, powerful, and easier to read.

Practice Time!

  1. Create a tuple to store the days of the week. Try to change the first day to "Funday" and see what error you get.

  2. Create a dictionary that stores information about your favorite book (e.g., keys for "title", "author", "year_published").

  3. Print the author of the book from your dictionary.

  4. Add a new key-value pair to your book dictionary, for example, "genre": "Fantasy". Then, use a for loop to print all the information about the book in a readable format.

Last updated