Python Dictionary Methods Flashcards

(26 cards)

1
Q

What does the get() method do in a dictionary?

A

It retrieves the value for a specified key, returning None if the key is not found.

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

Define keys() method.

A

Returns a view object displaying a list of all the keys in the dictionary.

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

What does the values() method return?

A

It returns a view object displaying a list of all the values in the dictionary.

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

Fill in the blank: The items() method returns _______.

A

A view object of key-value pairs in the dictionary.

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

True or false: The update() method adds key-value pairs to a dictionary.

A

TRUE

It can also update existing keys with new values.

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

What does the pop() method do?

A

Removes and returns the value associated with a specified key.

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

Define popitem() method.

A

Removes and returns the last inserted key-value pair as a tuple.

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

What is the purpose of the clear() method?

A

It removes all items from the dictionary.

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

Fill in the blank: The setdefault() method returns the value of a key, and if not present, it sets it to _______.

A

A default value.

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

True or false: The copy() method creates a shallow copy of the dictionary.

A

TRUE

Changes to the copy do not affect the original dictionary.

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

What does the fromkeys() method do?

A

Creates a new dictionary with specified keys and a default value.

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

Define del statement in the context of dictionaries.

A

It removes a specified key and its associated value from the dictionary.

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

What is the result of using len() on a dictionary?

A

It returns the number of key-value pairs in the dictionary.

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

Fill in the blank: The in keyword checks if a key is _______ in a dictionary.

A

Present.

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

True or false: Dictionary keys can be mutable types.

A

FALSE

Only immutable types like strings, numbers, and tuples can be keys.

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

What does the dict() constructor do?

A

Creates a new dictionary from an iterable or keyword arguments.

17
Q

Define dict comprehension.

A

A concise way to create dictionaries using a single line of code.

18
Q

What is the purpose of the update() method?

A

It merges another dictionary or iterable of key-value pairs into the current dictionary.

19
Q

Fill in the blank: The get() method can take a second argument as a _______.

A

Default value.

20
Q

True or false: Dictionary values can be of any data type.

A

TRUE

Values can be strings, lists, dictionaries, etc.

21
Q

What does the view() method return?

A

It returns a view object that displays a dynamic view of the dictionary’s entries.

22
Q

Define nested dictionaries.

A

Dictionaries that contain other dictionaries as values.

23
Q

What is the output of d.get(‘key’, ‘default’) if ‘key’ is not in d?

A

It returns ‘default’.

24
Q

Fill in the blank: The keys() method returns a _______ object.

25
True or false: You can iterate through a dictionary using a for loop.
TRUE ## Footnote Iteration defaults to keys, but can also access values or items.
26
What does the **setdefault()** method return if the key exists?
It returns the existing value for that key.