Python Dictionary Methods Flashcards

1
Q

clear()

A

The clear() method removes all the elements from a dictionary.

dictionary.clear()

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

car.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

copy()

A

The copy() method returns a copy of the specified dictionary.

dictionary.copy()

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.copy()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

fromkeys()

A

The fromkeys() method returns a dictionary with the specified keys and the specified value.

dict.fromkeys(keys, value)

x = ('key1', 'key2', 'key3')
y = 0

thisdict = dict.fromkeys(x, y)

print(thisdict)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

get()

A

The get() method returns the value of the item with the specified key.

dictionary.get(keyname, value)

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.get(“model”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

items()

A

The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list.

dictionary.items()

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.items()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

keys()

A

The keys() method returns a view object. The view object contains the keys of the dictionary, as a list.

dictionary.keys()

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.keys()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pop()

A

The pop() method removes the specified item from the dictionary.

dictionary.pop(keyname, defaultvalue)

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

car.pop(“model”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

popitem()

A

The popitem() method removes the item that was last inserted into the dictionary. In versions before 3.7, the popitem() method removes a random item.

dictionary.popitem()

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

car.popitem()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

setdefault()

A

The setdefault() method returns the value of the item with the specified key.

dictionary.setdefault(keyname, value)

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.setdefault(“model”, “Bronco”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

update()

A

The update() method inserts the specified items to the dictionary.

dictionary.update(iterable)

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

car.update({“color”: “White”})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

values()

A

The values() method returns a view object. The view object contains the values of the dictionary, as a list.

dictionary.values()

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.values()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly