4 Python Dictionaries Flashcards

(15 cards)

1
Q

What is a Dictionary in Python?

A

A dictionary is a collection of key-value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

It’s used to store data where each value is accessed using a key, not an index (like in lists).

my_dict = {
“name”: “Durga”,
“goal”: “AI Scientist”,
“language”: “Python”
}

“name”, “goal” → keys

“Durga”, “AI Scientist” → values

print(my_dict[“name”]) # Output: Durga

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

Why are Dictionaries important in NLP?

A

They’re super useful for:

Counting word frequencies ({“the”: 10, “cat”: 2})

Mapping labels or tokens ({“positive”: 1, “negative”: 0})

Storing configurations or parameters

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

Dictionary Items - Data Types

A

thisdict = {
“brand”: “Ford”,
“electric”: False,
“year”: 1964,
“colors”: [“red”, “white”, “blue”]
}

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

What are dictionary methods

A

clear() – Removes all key-value pairs from the dictionary.

copy() – Returns a shallow copy of the dictionary.

fromkeys(keys, value) – Creates a new dictionary from given keys, all set to the same value.

get(key, default) – Retrieves the value of a key, returns default if key not found.

items() – Returns all key-value pairs as a view of tuples.

keys() – Returns all the keys in the dictionary.

pop(key) – Removes a key and returns its value.

popitem() – Removes and returns the last inserted key-value pair.

setdefault(key, default) – Returns value if key exists, else adds key with default value.

update(dict2) – Merges another dictionary into the current one.

values() – Returns all the values in the dictionary.

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

How do I remove all items from my dictionary?

A

d = {“a”: 1, “b”: 2}
d.clear()
print(d) # Output: {}

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

How do I make a duplicate of a dictionary?

A

original = {“x”: 10, “y”: 20}
clone = original.copy()
print(clone) # Output: {‘x’: 10, ‘y’: 20}

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

How do I create a new dictionary from a list of keys with the same value?

A

keys = [“name”, “age”, “city”]
new_dict = dict.fromkeys(keys, “unknown”)
print(new_dict)
# Output: {‘name’: ‘unknown’, ‘age’: ‘unknown’, ‘city’: ‘unknown’}

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

How can I safely get a value even if the key might not exist?

A

person = {“name”: “Durga”}
print(person.get(“age”)) # Output: None
print(person.get(“age”, 25)) # Output: 25

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

How do I get all key-value pairs as tuples?

A

d = {“x”: 1, “y”: 2}
for k, v in d.items():
print(k, v)
# Output: x 1 y 2

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

How can I get just the keys from a dictionary?

A

d = {“apple”: 5, “banana”: 7}
print(list(d.keys())) # Output: [‘apple’, ‘banana’]

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

How do I remove a specific key and get its value?

A

d = {“x”: 1, “y”: 2}
val = d.pop(“x”)
print(val) # Output: 1
print(d) # Output: {‘y’: 2}

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

How do I remove and return the last inserted item?

A

d = {“a”: 1, “b”: 2}
item = d.popitem()
print(item) # Output: (‘b’, 2)

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

How can I get a value if it exists, or set a default if it doesn’t?

A

d = {“x”: 10}
val = d.setdefault(“x”, 100)
print(val) # Output: 10 (because ‘x’ already exists)
print(d) # Output: {‘x’: 10}

d.setdefault(“y”, 200)
print(d) # Output: {‘x’: 10, ‘y’: 200}

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

How can I add multiple key-value pairs to a dictionary?

A

d = {“x”: 1}
d.update({“y”: 2, “z”: 3})
print(d) # Output: {‘x’: 1, ‘y’: 2, ‘z’: 3}

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

How do I get all the values from a dictionary?

A

d = {“a”: 10, “b”: 20}
print(list(d.values())) # Output: [10, 20]

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