Loading video player…

Updating a Map With set() and delete()

Updating a Map With set() and delete()

Ola Bakare, member 318, has applied for a plot, and plot 3 goes to the first person on the waiting list, who then leaves it. In this lesson, you ported Rosa’s two changes to the waiting list, which is a Map, and saw why the brackets you’d use on an object don’t change it.

Here’s the step in the Python script:

Language: Python
GIVEN_UP = 3
waiting = {
    214: "Dev Patel",
    187: "Hannah Cole",
    305: "Marek Nowak",
    42: "June Harris",
}

waiting[318] = "Ola Bakare"
member = next(iter(waiting))
name = waiting.pop(member)

names = ", ".join(waiting.values())
print(f"Plot {GIVEN_UP} goes to {name} (member {member})")
print(f"Waiting list: {names}")
  • Python prints Plot 3 goes to Dev Patel (member 214) and Waiting list: Hannah Cole, Marek Nowak, June Harris, Ola Bakare.
  • On an object, square brackets add a key and the delete operator removes one, so a line-by-line port adds Ola with waiting[318] = "Ola Bakare";. A Map has no pop(), so the port reads the name with waiting.get(member) and then writes delete waiting[member];.
  • The port runs without an error, and its first line matches Python’s. But the second is Waiting list: Dev Patel, Hannah Cole, Marek Nowak, June Harris: Dev Patel got the plot and kept his place, and Ola Bakare never joined the list.
  • A Map answers Python’s 318 in waiting with waiting.has(318) and len(waiting) with waiting.size, as a Set does. size is a property, so it takes no parentheses. The port’s Map has a size of 4, the old list unchanged, and has(318) is false.
  • Logging the Map shows the four old entries, each with =>, and after them '318': 'Ola Bakare', with a colon where the entries have an arrow, and a string key. That’s how Node logs an ordinary property, not an entry.
  • Like a dict’s items and attributes, a Map’s entries are apart from its properties, as the last lesson said, in a store that only its methods reach, such as get(), has() and the keys() you spread.
  • On a Map, square brackets reach properties, so they miss the store: waiting[318] = "Ola Bakare" wrote a property under the string '318', as objects store number keys, and delete waiting[214] looked for a property named '214', found none, still returned true, where Python’s del raises a KeyError, and changed nothing.

Here’s the port with the Map’s own methods:

Language: JavaScript
const GIVEN_UP = 3;
const waiting = new Map([
  [214, "Dev Patel"],
  [187, "Hannah Cole"],
  [305, "Marek Nowak"],
  [42, "June Harris"],
]);

waiting.set(318, "Ola Bakare");
const member = [...waiting.keys()][0];
const name = waiting.get(member);
waiting.delete(member);

const names = [...waiting.values()].join(", ");
console.log(`Plot ${GIVEN_UP} goes to ${name} (member ${member})`);
console.log(`Waiting list: ${names}`);
console.log(waiting.size, waiting.has(318));

set() takes a key and a value and adds the entry. delete() takes a key and removes its entry, and it returns true if it removed an entry and false if not, never the value, so get() reads the name first: together, they do the work of Python’s pop(). The port now prints Python’s lines, and the checks agree:

Language: Program Output
Plot 3 goes to Dev Patel (member 214)
Waiting list: Hannah Cole, Marek Nowak, June Harris, Ola Bakare
4 true

set() on a key that’s already there replaces the value and keeps its place, as assigning to a dict key does, so waiting.set(187, "Hannah Cole-Byrne") leaves her first. A Map also keeps 318 a number, so waiting.has("318") is false, as Python’s "318" in waiting is False.

The rule to keep: a Map’s entries are reached only through its methods and size. set() adds or replaces, delete() removes, get() reads, has() checks, and size counts. Keys in square brackets are for objects.

Next up is the text lesson Choosing Between a Map and an Object, where you’ll decide when data belongs in an object and when in a Map, and how to get a Map into JSON and back.

00:02 In the last lesson, the waiting list became a Map, so it keeps the order people asked in. Now you’ll change that list. Ola Bakare, member 318, has applied for a plot.

00:16 In Python, square brackets add her at the end. Plot three is free, so iter() finds the first member. pop() removes that member and returns the name.

00:27 Python prints Dev Patel for plot three. The list now starts with Hannah Cole and ends with Ola. The port starts from the last lesson’s Map. On an object, square brackets add a key.

00:43 So the port adds Ola that way, as Python does. It finds the first member as before. A Map has no .pop(), so the port takes two steps. It reads the name with .get().

00:58 Then the delete operator, which removes a key from an object, takes the member off.

01:05 The port runs without an error. Its first line matches Python’s: Dev Patel gets plot three. What does it print for the waiting list? Your turn: pause the video and predict.

01:23 The list still starts with Dev Patel. And Ola Bakare is missing. Dev got the plot and kept his place, and Ola never joined the list.

01:37 Python checks a dict with in and len(). A Map answers with .has() and .size, as a Set does. .size is a property, so it takes no parentheses.

01:50 Python says 4 and True. The Map also counts 4, but the .has() check says false. Python’s 4 is one added and one removed.

02:02 The Map’s 4 is the old list, unchanged.

02:07 Where’s Ola? Log the Map, as you’d print a dict. Python shows four people, Ola last. JavaScript shows the four old entries, with arrows, Dev still first. Ola comes after them, with a colon, not an arrow, and her number in quotes.

02:27 That’s how Node logs a property, not an entry.

02:32 A dict’s items are apart from its attributes. As the last lesson said, a Map’s entries are apart from its properties too, in their own store. Only its methods reach that store, like .get(), .has() and .keys().

02:47 On a Map, square brackets reach properties, so they miss the store. The port wrote Ola as a property, under the string '318', as objects store number keys.

02:59 delete looked for a property named 214. There isn’t one, but it returns true anyway, where Python’s del raises a KeyError.

03:08 Dev’s entry stays in the store.

03:12 To change the entries, use the Map’s methods. .set() takes a key and a value, and adds the entry. The .delete() method takes a key and removes its entry.

03:24 It returns true if it removed an entry, false if not, never the name, so .get() reads the name first. Together, they do the work of Python’s pop().

03:36 Run it again, and the port prints Python’s lines. Hannah Cole now leads, and Ola is last. The .has() check says true, and the .size is 4.

03:49 .set() also replaces a value. If Hannah Cole changes her name, setting her number again keeps her first, as a dict does. And the Map keeps 318 a number.

04:02 So .has() with the string '318' says false, as Python’s in does.

04:10 The rule to keep: a Map’s entries are reached only through its methods and size. .set() adds or replaces, .delete() removes, .get() reads, .has() checks, and .size counts.

04:24 Keys in square brackets are for objects.

04:28 Next up is the text lesson Choosing Between a Map and an Object. You’ll choose between an object and a Map, and get a Map into JSON and back.

Become a Member to join the conversation.