Course 1.2Lesson 2 of 5
A set keeps each value only once, has no order, and so has no positions. set of a list removes the repeats. add puts a value in and does nothing if it is already there, and discard takes a value out without an error. The word in on a set is very fast. len counts the different values, and sorted makes a list in order to show a set neatly. An empty set is set, because the curly brackets alone make an empty dictionary. A set holds only fixed values, so it cannot hold a list.
Try the code and the simulations beside the lesson.
After this lesson you can
A set is a bag of values in which nothing is repeated. It does not keep an order, so there is no s[0]. A set is useful when you want to know what is there, and not how often or where.
Think of the list of names of the students who came to school today. A student who comes in twice is still one student. A set keeps the different names.
Predict first: the list [3, 1, 3, 2, 1] has repeats. What will len(unique) be? Then run it.
set(numbers) turned the list into a set with only 1, 2 and 3. So len(unique) is 3.sorted(unique) to show it in order. The result of sorted is a list.colours.add("blue") puts a value in. colours.add("red") again changes nothing, because red is there."green" in colours asks whether it is in the set. For a set, this is very fast, even if the set has a million values. For a list, Python looks at the items one by one.colours.discard("green") takes it out. It does not complain if the value is not there. (The method remove does complain, with a KeyError.){} makes an empty dictionary. An empty set is set(). This is the one place where Python is a little odd.Also, a set shows its items in an order that you cannot predict. Never depend on it. Use sorted() for a neat output, and you will get the same output on every computer.
| Call | What it does |
|---|---|
set(a_list) | makes a set, and drops the repeats |
s.add(x) | puts x in (nothing happens if it is already there) |
s.discard(x) | takes x out (no error if it is missing) |
s.remove(x) | takes x out (a KeyError if it is missing) |
x in s | asks if x is in the set, and it is fast |
len(s) | counts the different values |
sorted(s) | makes a list in order, so that you can show it neatly |
However many rolls you make, the set never has more than 6 items: a die has only six faces.
A set can hold only values that cannot change: numbers, text, tuples. It cannot hold a list or another set, for the same reason that a list cannot be a dictionary key. {[1, 2]} is a TypeError.
{} for an empty set. It is an empty dictionary. Use set().sorted().s[0]. A TypeError.s.remove(x) for a missing value. A KeyError. Use discard.TypeError.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
set(list) removes the repeats.add puts in, discard takes out without an error. x in s is a quick test.set() is an empty set. {} is an empty dictionary.sorted(s) to show it in order.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
len({1, 2, 2, 3, 3, 3})?x in a_list or x in a_set?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.