4 Python Dictionaries Flashcards
(15 cards)
What is a Dictionary in Python?
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
Why are Dictionaries important in NLP?
They’re super useful for:
Counting word frequencies ({“the”: 10, “cat”: 2})
Mapping labels or tokens ({“positive”: 1, “negative”: 0})
Storing configurations or parameters
Dictionary Items - Data Types
thisdict = {
“brand”: “Ford”,
“electric”: False,
“year”: 1964,
“colors”: [“red”, “white”, “blue”]
}
What are dictionary methods
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 do I remove all items from my dictionary?
d = {“a”: 1, “b”: 2}
d.clear()
print(d) # Output: {}
How do I make a duplicate of a dictionary?
original = {“x”: 10, “y”: 20}
clone = original.copy()
print(clone) # Output: {‘x’: 10, ‘y’: 20}
How do I create a new dictionary from a list of keys with the same value?
keys = [“name”, “age”, “city”]
new_dict = dict.fromkeys(keys, “unknown”)
print(new_dict)
# Output: {‘name’: ‘unknown’, ‘age’: ‘unknown’, ‘city’: ‘unknown’}
How can I safely get a value even if the key might not exist?
person = {“name”: “Durga”}
print(person.get(“age”)) # Output: None
print(person.get(“age”, 25)) # Output: 25
How do I get all key-value pairs as tuples?
d = {“x”: 1, “y”: 2}
for k, v in d.items():
print(k, v)
# Output: x 1 y 2
How can I get just the keys from a dictionary?
d = {“apple”: 5, “banana”: 7}
print(list(d.keys())) # Output: [‘apple’, ‘banana’]
How do I remove a specific key and get its value?
d = {“x”: 1, “y”: 2}
val = d.pop(“x”)
print(val) # Output: 1
print(d) # Output: {‘y’: 2}
How do I remove and return the last inserted item?
d = {“a”: 1, “b”: 2}
item = d.popitem()
print(item) # Output: (‘b’, 2)
How can I get a value if it exists, or set a default if it doesn’t?
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 can I add multiple key-value pairs to a dictionary?
d = {“x”: 1}
d.update({“y”: 2, “z”: 3})
print(d) # Output: {‘x’: 1, ‘y’: 2, ‘z’: 3}
How do I get all the values from a dictionary?
d = {“a”: 10, “b”: 20}
print(list(d.values())) # Output: [10, 20]