Tuples, Sets, and Dictionaries
# 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.
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).
*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.
5. Nested Data Structures
In data science, structures are often nested. A JSON response from a web server is usually a List of Dictionaries.
6. Dictionary Comprehensions
Just like List Comprehensions, you can create dictionaries in a single line.
7. Mini Project: Student Records Manager
Let's use a dictionary to manage student grades and calculate their average.
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
Which data structure is Immutable (cannot be changed)?
Which symbols are used to define a Tuple?
If you need to quickly remove duplicates from a massive list, you should convert it to a:
What is the output of {1, 1, 2, 2, 3}?
Dictionaries store data in what format?
How do you access the value associated with the key "age" in a dictionary named user?
What method should you use to access a dictionary key safely without causing a KeyError if it doesn't exist?
Which of the following is an example of a Set?
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.