| Method | Meaning |
|---|---|
| clear() | appends an element to the end of the list. |
| copy() | returns a copy of the specified dictionary. |
| items() | returns the key-value pairs of the dictionary, as tuples in a list. |
| keys() | returns the keys of the dictionary, as a list.. |
| values() | returns the values of the dictionary, as a list. |
| update() | inserts an item or items to the dictionary. |
| fromkeys() | returns a dictionary with the specified keys and the specified value. |
| get() | returns the value of the item with the specified key |
| pop() | removes the last key-value pair in a dictionary. |
| popitem() | removes the item that was last inserted into the dictionary. |
| setdefault() | returns the value of the item with the specified key. |
clear()¶purdue = {
"location": "West Lafayette",
"state": "Indiana",
"founded": 1869
}
purdue.clear()
print(purdue)
{}
copy()¶purdue = {
"location": "West Lafayette",
"state": "Indiana",
"founded": 1869
}
x = purdue.copy()
x['location'] = 'Tucson'
print(x)
print(purdue)
{'location': 'Tucson', 'state': 'Indiana', 'founded': 1869}
{'location': 'West Lafayette', 'state': 'Indiana', 'founded': 1869}
items()¶purdue = {
"location": "West Lafayette",
"state": "Indiana",
"founded": 1869
}
x = purdue.items()
print(x)
for item in purdue.items():
print(item)
dict_items([('location', 'West Lafayette'), ('state', 'Indiana'), ('founded', 1869)])
('location', 'West Lafayette')
('state', 'Indiana')
('founded', 1869)
# here we changed the year from 1869 to 2023
purdue = {
"location": "West Lafayette",
"state": "Indiana",
"founded": 1869
}
x = purdue.items()
purdue["founded"] = 2023
print(x)
keys()¶info = {
"vowels": "aeiou",
"consonants": "abcdfghjklmnpqrstvwxyz",
}
x = info.keys()
print(x)
info = {
"vowels": "aeiou",
"consonants": "abcdfghjklmnpqrstvwxyz",
}
x = info.keys()
info["language"] = "English"
print(x)
values()¶info = {
"vowels": "aeiou",
"consonants": "abcdfghjklmnpqrstvwxyz",
}
x = info.values()
print(x)
info = {
"vowels": "aeiou",
"consonants": "abcdfghjklmnpqrstvwxyz",
}
x = info.values()
info["language"] = "English"
print(x)
update(iterable)¶iterable (a dictionary or an iterable with key and value)info = {
"vowels": "aeiou",
"consonants": "abcdfghjklmnpqrstvwxyz",
}
info.update({"language": "English"})
print(info)
fromkeys(keys, value)¶keys (required) and value (optional).k = ['1', '2', '3']
v = "FFFF"
d = dict.fromkeys(k, v)
print(d)
get(keyname, value)¶keyname (required) and value (optional).sentence = {
"pronoun": "I",
"verb": "love",
"propernoun": "python"
}
x = sentence.get("verb")
print(x)
pop(keyname, defaultvalue)¶keyname (required) and defaultvalue (optional).sentence = {
"pronoun": "I",
"verb": "love",
"propernoun": "python"
}
x = sentence.pop("pronoun")
sentence
pipitem()¶sentence = {
"pronoun": "I",
"verb": "love",
"propernoun": "python"
}
sentence.popitem()
print(sentence)
setdefault(keyname, value)¶keyname (required) and value (optional)car = {
"brand": "Honda",
"model": "Accord",
"color": "Gold"
}
res = car.setdefault("model")
res