Lists, Tuples, Sets and Dictionary - Study Notes
Chapter Summary
Python has four built-in collection types, and this chapter covers all of them: List, Tuple, Set and Dictionary. Each one lets you keep many values under a single name. A List is an ordered sequence that can be changed. A Tuple is an ordered sequence that cannot be changed. A Set holds only unique elements and keeps no fixed order. A Dictionary stores key-value pairs, where each key leads to one value. Most exam questions in this chapter come down to one thing: which of these can be changed, and which brackets are used to write it.
Learning Objectives
- State how Lists, Tuples, Sets and Dictionaries are alike and how they differ.
- Create and change Lists using append, extend, insert and remove.
- Work with Tuples, knowing that they are immutable, and use tuple assignment.
- Carry out set operations: Union, Intersection, Difference and Symmetric Difference.
- Create a Dictionary and access, update and delete its elements using keys.
- Explain why Tuples are faster to process than Lists.
Key Concepts and Definitions
Lists
A List is a sequence of elements written inside square brackets [ ]. It is ordered, so each element keeps the position it was placed in. It is mutable, so elements can be replaced, added or deleted while the program runs. Positive indexes start at 0 from the front. Negative indexes start at -1 from the end, which is called reverse indexing.
Tuples
A Tuple is a sequence of elements separated by commas, usually written inside parentheses ( ), though the parentheses are optional. It is ordered but immutable: once created, its elements cannot be changed. Because Python does not have to allow for changes, a Tuple is processed faster than a List.
Sets
A Set is a collection of unique elements written inside curly braces { }, with no fixed order. Duplicate values are not kept. Sets are handy for checking whether a value is present and for removing repeated values from a sequence. Note that empty braces {} create an empty Dictionary, not a Set; use set() for an empty Set.
Dictionaries
A Dictionary is a mutable collection of key-value pairs written inside curly braces { }. A colon (:) separates each key from its value, and every key must be unique. The key works like your own index, so you look up a value by its key instead of by position.
Worked Methods
1. Creating and Updating Lists
Create a List with square brackets. Use append() to add one element at the end and extend() to add several elements at once. Use insert(index, element) to place an element at a given position. To change an existing element, assign to its index: list_var[index] = new_value.
2. List Comprehension
List comprehension builds a new list in a single line. The general form is [expression for variable in range]. For example, squares = [x**2 for x in range(1, 11)] gives the squares of 1 to 10.
3. Tuple Assignment
In tuple assignment, the variables on the left of = receive the values on the right, in order. For example, (x, y, z) = (10, 20, 30) puts 10 in x, 20 in y and 30 in z.
4. Set Operations
Sets support the operations you know from mathematics:
- Union (|): all elements from both sets, each listed once.
- Intersection (&): only the elements found in both sets.
- Difference (-): elements in the first set that are not in the second.
- Symmetric Difference (^): elements in either set but not in both.
Common Exam Traps
- The Singleton Tuple: a one-element tuple needs a trailing comma, as in t = (10,). Without the comma, t = (10) is simply the integer 10 in brackets.
- Tuple Immutability: a statement such as t[0] = 5 on a tuple raises a TypeError. You cannot change or delete a single element, but you can delete the whole tuple with the del statement.
- Set Duplication: putting a duplicate into a set does not cause an error. Python just keeps one copy, so {1, 2, 2, 3} becomes {1, 2, 3}.
- Dictionary Key Uniqueness: keys must be unique. If you assign a value to a key that already exists, the old value is replaced with no warning.
Exam Tips
- In range(start, stop, step), counting stops at stop - 1. So range(1, 11) gives 1 to 10, not 1 to 11.
- Negative indexing is the quickest way to reach elements from the end of a list or tuple. Index -1 is always the last element.
- Look closely at the brackets in a question: [] means a List, () means a Tuple, and {} means a Set or a Dictionary depending on whether the items are key:value pairs.
- If you are unsure what kind of object a variable holds, type() will tell you.