Python Dict Methods Flashcards

1
Q

dict[key] = value

A

add or update a value in dictionary

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

update()

A

updates the dict with the key-value pairs from another dictionary or iterable.
# Initial dictionary
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}

Dictionary with updates
updates = {‘b’: 20, ‘d’: 4}

Update my_dict with updates
my_dict.update(updates)

print(my_dict) # Output: {‘a’: 1, ‘b’: 20, ‘c’: 3, ‘d’: 4}

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

get()

A

Returns the value of a specified key

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

keys()

A

Returns a list of containing the keys of the dict.

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

values()

A

Returns a list of the values of a dict.

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

items()

A

Returns a list of keys and values containing the key-value pairs of the dict as tuples.

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

pop()

A

Removes and returns the value associated with the specified key

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

del dict[key]

A

delete the item with the specified key

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

in/ not in

A

checking the membership

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

copy()

A

Returns the shallow copy of the dictionary

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

clear()

A

Removes all items from the dictionary

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

len()

A

Returns the number of items in the dictionary.

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

sorted()

A

Returns a new dictionary sorted by keys

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