dictionary methods Flashcards

(11 cards)

1
Q

Method to get a list of all keys in a dictionary

A

.keys() (returns a view object)

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

Method to get a list of all values in a dictionary

A

.values() (returns a view object)

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

Method to get a list of (key, value) tuples

A

.items() (returns a view object)

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

Method to remove a key-value pair using the key and return its value

A

.pop(key, [default])

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

Method to remove and return an arbitrary (random) key-value pair

A

.popitem()

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

Method to clear all key-value pairs from the dictionary

A

.clear()

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

Method to get the value for a given key, or a default value if the key is not found

A

.get(key, [default])

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

Method to insert key-value pairs from another dictionary or iterable into the current dictionary

A

.update(other_dict)

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

Method to return a new dictionary with specified keys and a default value for each

A

.fromkeys(iterable, [value])

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

Method to insert a key with a value if the key does not already exist

A

.setdefault(key, [default_value])

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

What is the time complexity for looking up a key in a Python dictionary?

A

O(1) (Constant time)

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